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 =
{"1.12" => {"us-west-2" => "ami-0923e4b35a30a5f53",
"us-east-1" => "ami-0abcb9f9190e867ab",
"us-east-2" => "ami-04ea7cb66af82ae4a",
"us-west-1" => "ami-03612357ac9da2c7d"},
            "1.13" => {"us-west-2" => "ami-089d3b6350c1769a6",
"us-east-1" => "ami-08c4955bcc43b124e",
"us-east-2" => "ami-07ebcae043cf995aa"}}
GPU_AMIS =
{"1.12" => {"us-west-2" => "ami-0bebf2322fd52a42e",
"us-east-1" => "ami-0cb7959f92429410a",
"us-east-2" => "ami-0118b61dc2312dee2",
"us-west-1" => "ami-047637529a86c7237"},
                "1.13" => {"us-west-2" => "ami-08e5329e1dbf22c6a",
"us-east-1" => "ami-02af865c0f3b337f2",
"us-east-2" => "ami-01f82bb66c17faf20"}}
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.



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

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

Instance Method Details

#asgObject



81
82
83
# File 'lib/eks_cli/nodegroup.rb', line 81

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

#cf_stackObject



102
103
104
105
106
107
# File 'lib/eks_cli/nodegroup.rb', line 102

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



56
57
58
59
60
61
62
63
64
# File 'lib/eks_cli/nodegroup.rb', line 56

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



73
74
75
76
77
78
79
# File 'lib/eks_cli/nodegroup.rb', line 73

def delete
  Log.info "deleting nodegroup #{@name}"
  cf_stack.delete
  if @group["spotinst"]
    spotinst.delete_elastigroup(@group["spotinst"]["id"])
  end
end

#export_to_spotinst(exact_instance_type) ⇒ Object



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

def export_to_spotinst(exact_instance_type)
  Log.info "exporting nodegroup #{@name} to spotinst"
  instance_types = exact_instance_type ? [instance_type] : nil
  response = spotinst.import_asg(config["region"], asg, instance_types)
  if response.code == 200
    Log.info "Successfully created elastigroup"
    elastigroup = response.parsed_response["response"]["items"].first
    config.update_nodegroup({"group_name" => @name, "spotinst" => elastigroup})
  else
    Log.warn "Error creating elastigroup:\n #{response}"
  end
end

#instance_typeObject



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

def instance_type
  @group["instance_type"]
end

#nameObject



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

def name; @name; end

#scale(min, max, asg = true, spotinst = false) ⇒ Object



109
110
111
112
# File 'lib/eks_cli/nodegroup.rb', line 109

def scale(min, max, asg = true, spotinst = false)
  scale_asg(min, max) if asg
  scale_spotinst(min, max) if spotinst
end

#tagsObject



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

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