Class: Kubecontrol::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/kubecontrol/client.rb

Constant Summary collapse

DEFAULT_NAMESPACE =
'default'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(namespace = DEFAULT_NAMESPACE) ⇒ Client

Returns a new instance of Client.



10
11
12
# File 'lib/kubecontrol/client.rb', line 10

def initialize(namespace = DEFAULT_NAMESPACE)
  @namespace = namespace
end

Instance Attribute Details

#namespaceObject

Returns the value of attribute namespace.



8
9
10
# File 'lib/kubecontrol/client.rb', line 8

def namespace
  @namespace
end

Instance Method Details

#apply(file_path: nil, kustomization_dir: nil) ⇒ Object

Raises:

  • (ArgumentError)


14
15
16
17
18
19
20
21
22
# File 'lib/kubecontrol/client.rb', line 14

def apply(file_path: nil, kustomization_dir: nil)
  raise ArgumentError.new('Must pass a file_path or kustomization_dir keyword argument') if (file_path.nil? && kustomization_dir.nil?) || (file_path && kustomization_dir)

  if file_path
    kubectl_command("apply -f #{file_path}")
  else
    kubectl_command("apply -k #{kustomization_dir}")
  end
end

#deploymentsObject



28
29
30
# File 'lib/kubecontrol/client.rb', line 28

def deployments
  get_resource(Resources::Deployment, 5)
end

#find_deployment_by_name(name_regex) ⇒ Object



56
57
58
# File 'lib/kubecontrol/client.rb', line 56

def find_deployment_by_name(name_regex)
  deployments.find { |deployment| deployment.name.match?(name_regex) }
end

#find_pod_by_name(name_regex) ⇒ Object



52
53
54
# File 'lib/kubecontrol/client.rb', line 52

def find_pod_by_name(name_regex)
  pods.find { |pod| pod.name.match?(name_regex) }
end

#find_secret_by_name(name_regex) ⇒ Object



44
45
46
# File 'lib/kubecontrol/client.rb', line 44

def find_secret_by_name(name_regex)
  secrets.find { |secret| secret.name.match?(name_regex) }
end

#find_service_by_name(name_regex) ⇒ Object



48
49
50
# File 'lib/kubecontrol/client.rb', line 48

def find_service_by_name(name_regex)
  services.find { |service| service.name.match?(name_regex) }
end

#find_stateful_set_by_name(name_regex) ⇒ Object



60
61
62
# File 'lib/kubecontrol/client.rb', line 60

def find_stateful_set_by_name(name_regex)
  stateful_sets.find { |stateful_set| stateful_set.name.match?(name_regex) }
end

#kubectl_command(command) ⇒ Object



64
65
66
67
68
69
# File 'lib/kubecontrol/client.rb', line 64

def kubectl_command(command)
  stdout_data, stderr_data, status = Open3.capture3("kubectl -n #{namespace} #{command}")
  exit_code = status.exitstatus

  [stdout_data, stderr_data, exit_code]
end

#podsObject



24
25
26
# File 'lib/kubecontrol/client.rb', line 24

def pods
  get_resource(Resources::Pod, 5)
end

#secretsObject



40
41
42
# File 'lib/kubecontrol/client.rb', line 40

def secrets
  get_resource(Resources::Secret, 4)
end

#servicesObject



36
37
38
# File 'lib/kubecontrol/client.rb', line 36

def services
  get_resource(Resources::Service, 6)
end

#stateful_setsObject



32
33
34
# File 'lib/kubecontrol/client.rb', line 32

def stateful_sets
  get_resource(Resources::StatefulSet, 3)
end