Class: EksCli::NodeGroup

Inherits:
Object
  • Object
show all
Defined in:
lib/eks_cli/nodegroup.rb

Constant Summary collapse

T =
{cluster_name: "ClusterName",
control_plane_sg_id: "ClusterControlPlaneSecurityGroup",
nodes_sg_id: "ClusterSecurityGroup",
min: "NodeAutoScalingGroupMinSize",
max: "NodeAutoScalingGroupMaxSize",
desired: "NodeAutoScalingGroupDesiredCapacity",
instance_type: "NodeInstanceType",
ami: "NodeImageId",
volume_size: "NodeVolumeSize",
ssh_key_name: "KeyName",
vpc_id: "VpcId",
subnets: "Subnets",
group_name: "NodeGroupName",
iam_policies: "NodeGroupIAMPolicies",
bootstrap_args: "BootstrapArguments"}
AMIS =
{"us-west-2" => "ami-0923e4b35a30a5f53",
"us-east-1" => "ami-0abcb9f9190e867ab",
"us-east-2" => "ami-04ea7cb66af82ae4a",
"us-west-1" => "ami-03612357ac9da2c7d"}
GPU_AMIS =
{"us-west-2" => "ami-0bebf2322fd52a42e",
"us-east-1" => "ami-0cb7959f92429410a",
"us-east-2" => "ami-0118b61dc2312dee2",
"us-west-1" => "ami-047637529a86c7237"}
EKS_IAM_POLICIES =
%w{AmazonEKSWorkerNodePolicy
AmazonEKS_CNI_Policy
AmazonEC2ContainerRegistryReadOnly}
CAPABILITIES =
["CAPABILITY_IAM"]

Instance Method Summary collapse

Constructor Details

#initialize(cluster_name, name) ⇒ NodeGroup

Returns a new instance of NodeGroup.



44
45
46
47
48
# File 'lib/eks_cli/nodegroup.rb', line 44

def initialize(cluster_name, name)
  @cluster_name = cluster_name
  @name = name
  @group = Config[cluster_name].for_group(name)
end

Instance Method Details

#asgObject



69
70
71
# File 'lib/eks_cli/nodegroup.rb', line 69

def asg
  @asg ||= cf_stack.resource("NodeGroup")
end

#cf_stackObject



83
84
85
86
87
88
# File 'lib/eks_cli/nodegroup.rb', line 83

def cf_stack
  CloudFormation::Stack.find(@cluster_name, stack_name)
rescue Aws::CloudFormation::Errors::ValidationError => e
  Log.error("could not find stack for nodegroup #{@name} - please make sure to run eks create-nodegroup --all --yes -c <cluster_name> to sync config")
  raise e
end

#create(wait_for_completion: true) ⇒ Object



50
51
52
53
54
55
56
57
58
# File 'lib/eks_cli/nodegroup.rb', line 50

def create(wait_for_completion: true)
  Log.info "creating stack for nodegroup #{@group["group_name"]}"
  stack = CloudFormation::Stack.create(@cluster_name, cloudformation_config)
  Log.info "stack created - #{@group["group_name"]} - #{stack.id}"
  if wait_for_completion
    await(stack)
  end
  stack
end

#deleteObject



65
66
67
# File 'lib/eks_cli/nodegroup.rb', line 65

def delete
  cf_stack.delete
end

#export_to_spotinst(exact_instance_type) ⇒ Object



77
78
79
80
81
# File 'lib/eks_cli/nodegroup.rb', line 77

def export_to_spotinst(exact_instance_type)
  Log.info "exporting nodegroup #{@name} to spotinst"
  instance_types = exact_instance_type ? [instance_type] : nil
  Log.info Spotinst::Client.new.import_asg(config["region"], asg, instance_types)
end

#instance_typeObject



73
74
75
# File 'lib/eks_cli/nodegroup.rb', line 73

def instance_type
  @group["instance_type"]
end

#scale(min, max) ⇒ Object



90
91
92
93
94
95
96
97
# File 'lib/eks_cli/nodegroup.rb', line 90

def scale(min, max)
  Log.info "scaling #{asg}: min -> #{min}, max -> #{max}"
  Log.info asg_client.update_auto_scaling_group({
    auto_scaling_group_name: asg, 
    max_size: max, 
    min_size: min
  })
end

#tagsObject



60
61
62
63
# File 'lib/eks_cli/nodegroup.rb', line 60

def tags
  [{key: "eks-nodegroup", value: @group["group_name"]},
   {key: "eks-cluster", value: @cluster_name}]
end