Class: Krane::KubeclientBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/krane/kubeclient_builder.rb

Defined Under Namespace

Classes: ContextMissingError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(kubeconfig: nil) ⇒ KubeclientBuilder

Returns a new instance of KubeclientBuilder.



15
16
17
18
19
# File 'lib/krane/kubeclient_builder.rb', line 15

def initialize(kubeconfig: nil)
  files = kubeconfig || ENV["KUBECONFIG"] || "#{Dir.home}/.kube/config"
  # Split the list by colon for Linux and Mac, and semicolon for Windows.
  @kubeconfig_files = files.split(/[:;]/).map!(&:strip).reject(&:empty?)
end

Instance Attribute Details

#kubeconfig_filesObject (readonly)

Returns the value of attribute kubeconfig_files.



13
14
15
# File 'lib/krane/kubeclient_builder.rb', line 13

def kubeconfig_files
  @kubeconfig_files
end

Instance Method Details

#build_apiextensions_v1beta1_kubeclient(context) ⇒ Object



68
69
70
71
72
73
74
# File 'lib/krane/kubeclient_builder.rb', line 68

def build_apiextensions_v1beta1_kubeclient(context)
  build_kubeclient(
    api_version: "v1beta1",
    context: context,
    endpoint_path: "/apis/apiextensions.k8s.io"
  )
end

#build_apps_v1_kubeclient(context) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/krane/kubeclient_builder.rb', line 60

def build_apps_v1_kubeclient(context)
  build_kubeclient(
    api_version: "v1",
    context: context,
    endpoint_path: "/apis/apps"
  )
end

#build_autoscaling_v1_kubeclient(context) ⇒ Object



76
77
78
79
80
81
82
# File 'lib/krane/kubeclient_builder.rb', line 76

def build_autoscaling_v1_kubeclient(context)
  build_kubeclient(
    api_version: "v2beta1",
    context: context,
    endpoint_path: "/apis/autoscaling"
  )
end

#build_batch_v1_kubeclient(context) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/krane/kubeclient_builder.rb', line 44

def build_batch_v1_kubeclient(context)
  build_kubeclient(
    api_version: "v1",
    context: context,
    endpoint_path: "/apis/batch/"
  )
end

#build_batch_v1beta1_kubeclient(context) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/krane/kubeclient_builder.rb', line 36

def build_batch_v1beta1_kubeclient(context)
  build_kubeclient(
    api_version: "v1beta1",
    context: context,
    endpoint_path: "/apis/batch/"
  )
end

#build_networking_v1_kubeclient(context) ⇒ Object



92
93
94
95
96
97
98
# File 'lib/krane/kubeclient_builder.rb', line 92

def build_networking_v1_kubeclient(context)
  build_kubeclient(
    api_version: "v1",
    context: context,
    endpoint_path: "/apis/networking.k8s.io"
  )
end

#build_policy_v1beta1_kubeclient(context) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/krane/kubeclient_builder.rb', line 52

def build_policy_v1beta1_kubeclient(context)
  build_kubeclient(
    api_version: "v1beta1",
    context: context,
    endpoint_path: "/apis/policy/"
  )
end

#build_rbac_v1_kubeclient(context) ⇒ Object



84
85
86
87
88
89
90
# File 'lib/krane/kubeclient_builder.rb', line 84

def build_rbac_v1_kubeclient(context)
  build_kubeclient(
    api_version: "v1",
    context: context,
    endpoint_path: "/apis/rbac.authorization.k8s.io"
  )
end

#build_scheduling_v1beta1_kubeclient(context) ⇒ Object



108
109
110
111
112
113
114
# File 'lib/krane/kubeclient_builder.rb', line 108

def build_scheduling_v1beta1_kubeclient(context)
  build_kubeclient(
    api_version: "v1beta1",
    context: context,
    endpoint_path: "/apis/scheduling.k8s.io"
  )
end

#build_storage_v1_kubeclient(context) ⇒ Object



100
101
102
103
104
105
106
# File 'lib/krane/kubeclient_builder.rb', line 100

def build_storage_v1_kubeclient(context)
  build_kubeclient(
    api_version: "v1",
    context: context,
    endpoint_path: "/apis/storage.k8s.io"
  )
end

#build_v1_kubeclient(context) ⇒ Object



21
22
23
24
25
26
# File 'lib/krane/kubeclient_builder.rb', line 21

def build_v1_kubeclient(context)
  build_kubeclient(
    api_version: "v1",
    context: context
  )
end

#build_v1beta1_kubeclient(context) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/krane/kubeclient_builder.rb', line 28

def build_v1beta1_kubeclient(context)
  build_kubeclient(
    api_version: "v1beta1",
    context: context,
    endpoint_path: "/apis/extensions/"
  )
end

#validate_config_filesObject



116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/krane/kubeclient_builder.rb', line 116

def validate_config_files
  errors = []
  if @kubeconfig_files.empty?
    errors << "Kubeconfig file name(s) not set in $KUBECONFIG"
  else
    @kubeconfig_files.each do |f|
      # If any files in the list are not valid, we can't be sure the merged context list is what the user intended
      errors << "Kubeconfig not found at #{f}" unless File.file?(f)
    end
  end
  errors
end

#validate_config_files!Object



129
130
131
132
# File 'lib/krane/kubeclient_builder.rb', line 129

def validate_config_files!
  errors = validate_config_files
  raise TaskConfigurationError, errors.join(', ') if errors.present?
end