Class: Krane::KubeclientBuilder

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

Defined Under Namespace

Classes: ContextMissingError

Instance Method Summary collapse

Constructor Details

#initialize(kubeconfig: ENV["KUBECONFIG"]) ⇒ KubeclientBuilder

Returns a new instance of KubeclientBuilder.



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

def initialize(kubeconfig: ENV["KUBECONFIG"])
  files = 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 Method Details

#build_apiextensions_v1beta1_kubeclient(context) ⇒ Object



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

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



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

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

#build_autoscaling_v1_kubeclient(context) ⇒ Object



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

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

#build_batch_v1_kubeclient(context) ⇒ Object



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

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

#build_batch_v1beta1_kubeclient(context) ⇒ Object



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

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

#build_networking_v1_kubeclient(context) ⇒ Object



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

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



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

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

#build_rbac_v1_kubeclient(context) ⇒ Object



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

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



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

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



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

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



19
20
21
22
23
24
# File 'lib/krane/kubeclient_builder.rb', line 19

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

#build_v1beta1_kubeclient(context) ⇒ Object



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

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

#validate_config_filesObject



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

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



127
128
129
130
# File 'lib/krane/kubeclient_builder.rb', line 127

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