Move external LB and target group to inline as well.

This commit is contained in:
Rodrigo Menezes 2020-08-27 16:17:52 +03:00 committed by Rodrigo Menezes
parent 1116a90575
commit 0456419c2e
10 changed files with 246 additions and 378 deletions

View File

@ -366,6 +366,16 @@ func (b *AutoscalingGroupModelBuilder) buildAutoScalingGroupTask(c *fi.ModelBuil
}
}
for _, extLB := range ig.Spec.ExternalLoadBalancers {
if extLB.LoadBalancerName != nil {
t.LoadBalancers = append(t.LoadBalancers, &awstasks.LoadBalancer{Name: extLB.LoadBalancerName})
}
if extLB.TargetGroupARN != nil {
t.TargetGroupARNs = append(t.TargetGroupARNs, &awstasks.TargetGroup{Name: extLB.TargetGroupARN, ARN: extLB.TargetGroupARN})
}
}
// @step: are we using a mixed instance policy
if ig.Spec.MixedInstancesPolicy != nil {
spec := ig.Spec.MixedInstancesPolicy
@ -386,20 +396,17 @@ func (b *AutoscalingGroupModelBuilder) buildAutoScalingGroupTask(c *fi.ModelBuil
func (b *AutoscalingGroupModelBuilder) buildExternalLoadBalancerTasks(c *fi.ModelBuilderContext, ig *kops.InstanceGroup) error {
for _, x := range ig.Spec.ExternalLoadBalancers {
if x.LoadBalancerName != nil {
c.AddTask(&awstasks.ExternalLoadBalancerAttachment{
Name: fi.String("extlb-" + *x.LoadBalancerName + "-" + ig.Name),
Lifecycle: b.Lifecycle,
LoadBalancerName: *x.LoadBalancerName,
AutoscalingGroup: b.LinkToAutoscalingGroup(ig),
c.AddTask(&awstasks.LoadBalancer{
Name: x.LoadBalancerName,
Shared: fi.Bool(true),
})
}
if x.TargetGroupARN != nil {
c.AddTask(&awstasks.ExternalTargetGroupAttachment{
Name: fi.String("exttg-" + *x.TargetGroupARN + "-" + ig.Name),
Lifecycle: b.Lifecycle,
TargetGroupARN: *x.TargetGroupARN,
AutoscalingGroup: b.LinkToAutoscalingGroup(ig),
c.AddTask(&awstasks.TargetGroup{
Name: x.TargetGroupARN,
ARN: x.TargetGroupARN,
Shared: fi.Bool(true),
})
}
}

View File

@ -80,27 +80,13 @@ provider "aws" {
region = "us-test-1"
}
resource "aws_autoscaling_attachment" "extlb-my-elb-nodes" {
autoscaling_group_name = aws_autoscaling_group.nodes-externallb-example-com.id
elb = "my-elb"
}
resource "aws_autoscaling_attachment" "extlb-my-other-elb-master-us-test-1a" {
autoscaling_group_name = aws_autoscaling_group.master-us-test-1a-masters-externallb-example-com.id
elb = "my-other-elb"
}
resource "aws_autoscaling_attachment" "exttg-aws_my-tg--0123456789abcdef-master-us-test-1a" {
alb_target_group_arn = "aws:arn:elasticloadbalancing:us-test-1a:123456789012:targetgroup/my-tg/0123456789abcdef"
autoscaling_group_name = aws_autoscaling_group.master-us-test-1a-masters-externallb-example-com.id
}
resource "aws_autoscaling_group" "master-us-test-1a-masters-externallb-example-com" {
enabled_metrics = ["GroupDesiredCapacity", "GroupInServiceInstances", "GroupMaxSize", "GroupMinSize", "GroupPendingInstances", "GroupStandbyInstances", "GroupTerminatingInstances", "GroupTotalInstances"]
launch_template {
id = aws_launch_template.master-us-test-1a-masters-externallb-example-com.id
version = aws_launch_template.master-us-test-1a-masters-externallb-example-com.latest_version
}
load_balancers = ["my-other-elb"]
max_size = 1
metrics_granularity = "1Minute"
min_size = 1
@ -140,6 +126,7 @@ resource "aws_autoscaling_group" "master-us-test-1a-masters-externallb-example-c
propagate_at_launch = true
value = "owned"
}
target_group_arns = ["aws:arn:elasticloadbalancing:us-test-1a:123456789012:targetgroup/my-tg/0123456789abcdef"]
vpc_zone_identifier = [aws_subnet.us-test-1a-externallb-example-com.id]
}
@ -149,6 +136,7 @@ resource "aws_autoscaling_group" "nodes-externallb-example-com" {
id = aws_launch_template.nodes-externallb-example-com.id
version = aws_launch_template.nodes-externallb-example-com.latest_version
}
load_balancers = ["my-elb"]
max_size = 2
metrics_granularity = "1Minute"
min_size = 2

View File

@ -18,10 +18,6 @@ go_library(
"ebsvolume_fitask.go",
"elastic_ip.go",
"elasticip_fitask.go",
"external_load_balancer_attachment.go",
"external_target_group_attachment.go",
"externalloadbalancerattachment_fitask.go",
"externaltargetgroupattachment_fitask.go",
"helper.go",
"iaminstanceprofile.go",
"iaminstanceprofile_fitask.go",
@ -67,6 +63,8 @@ go_library(
"subnet.go",
"subnet_fitask.go",
"tags.go",
"targetgroup.go",
"targetgroup_fitask.go",
"vpc.go",
"vpc_dhcpoptions_association.go",
"vpc_fitask.go",

View File

@ -87,6 +87,8 @@ type AutoscalingGroup struct {
SuspendProcesses *[]string
// Tags is a collection of keypairs to apply to the node on launch
Tags map[string]string
// TargetGroupARNs is a list of ALB/NLB ARNs to add to the autoscaling group
TargetGroupARNs []*TargetGroup
}
var _ fi.CompareWithID = &AutoscalingGroup{}
@ -118,6 +120,10 @@ func (e *AutoscalingGroup) Find(c *fi.Context) (*AutoscalingGroup, error) {
actual.LoadBalancers = append(actual.LoadBalancers, &LoadBalancer{Name: aws.String(*lb)})
}
for _, tg := range g.TargetGroupARNs {
actual.TargetGroupARNs = append(actual.TargetGroupARNs, &TargetGroup{ARN: aws.String(*tg)})
}
if g.VPCZoneIdentifier != nil {
subnets := strings.Split(*g.VPCZoneIdentifier, ",")
for _, subnet := range subnets {
@ -274,6 +280,10 @@ func (v *AutoscalingGroup) RenderAWS(t *awsup.AWSAPITarget, a, e, changes *Autos
request.LoadBalancerNames = append(request.LoadBalancerNames, k.GetName())
}
for _, tg := range e.TargetGroupARNs {
request.TargetGroupARNs = append(request.TargetGroupARNs, tg.ARN)
}
// @check are we using a launch configuration, mixed instances policy, or launch template
if e.LaunchConfiguration != nil {
request.LaunchConfigurationName = e.LaunchConfiguration.ID
@ -745,6 +755,7 @@ type terraformAutoscalingGroup struct {
SuspendedProcesses []*string `json:"suspended_processes,omitempty" cty:"suspended_processes"`
InstanceProtection *bool `json:"protect_from_scale_in,omitempty" cty:"protect_from_scale_in"`
LoadBalancers []*terraform.Literal `json:"load_balancers,omitempty" cty:"load_balancers"`
TargetGroupARNs []*terraform.Literal `json:"target_group_arns,omitempty" cty:"target_group_arns"`
}
// RenderTerraform is responsible for rendering the terraform codebase
@ -775,6 +786,10 @@ func (_ *AutoscalingGroup) RenderTerraform(t *terraform.TerraformTarget, a, e, c
tf.LoadBalancers = append(tf.LoadBalancers, k.TerraformLink())
}
for _, tg := range e.TargetGroupARNs {
tf.TargetGroupARNs = append(tf.TargetGroupARNs, tg.TerraformLink())
}
if e.LaunchConfiguration != nil {
tf.LaunchConfigurationName = e.LaunchConfiguration.TerraformLink()
} else if e.UseMixedInstancesPolicy() {
@ -1006,6 +1021,10 @@ func (_ *AutoscalingGroup) RenderCloudformation(t *cloudformation.Cloudformation
cf.LoadBalancerNames = append(cf.LoadBalancerNames, k.CloudformationLink())
}
for _, tg := range e.TargetGroupARNs {
cf.TargetGroupARNs = append(cf.TargetGroupARNs, tg.CloudformationLink())
}
return t.RenderResource("AWS::AutoScaling::AutoScalingGroup", fi.StringValue(e.Name), cf)
}

View File

@ -1,140 +0,0 @@
/*
Copyright 2019 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package awstasks
import (
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/autoscaling"
"k8s.io/klog/v2"
"k8s.io/kops/upup/pkg/fi"
"k8s.io/kops/upup/pkg/fi/cloudup/awsup"
"k8s.io/kops/upup/pkg/fi/cloudup/cloudformation"
"k8s.io/kops/upup/pkg/fi/cloudup/terraform"
)
// +kops:fitask
type ExternalLoadBalancerAttachment struct {
Name *string
Lifecycle *fi.Lifecycle
LoadBalancerName string
AutoscalingGroup *AutoscalingGroup
}
func (e *ExternalLoadBalancerAttachment) Find(c *fi.Context) (*ExternalLoadBalancerAttachment, error) {
cloud := c.Cloud.(awsup.AWSCloud)
if e.LoadBalancerName == "" {
return nil, fmt.Errorf("InstanceGroup did not have LoadBalancerNames set")
}
g, err := findAutoscalingGroup(cloud, *e.AutoscalingGroup.Name)
if err != nil {
return nil, err
}
if g == nil {
return nil, nil
}
for _, name := range g.LoadBalancerNames {
if aws.StringValue(name) != e.LoadBalancerName {
continue
}
actual := &ExternalLoadBalancerAttachment{}
actual.LoadBalancerName = e.LoadBalancerName
actual.AutoscalingGroup = e.AutoscalingGroup
// Prevent spurious changes
actual.Name = e.Name // ELB attachments don't have tags
actual.Lifecycle = e.Lifecycle
return actual, nil
}
return nil, nil
}
func (e *ExternalLoadBalancerAttachment) Run(c *fi.Context) error {
return fi.DefaultDeltaRunMethod(e, c)
}
func (s *ExternalLoadBalancerAttachment) CheckChanges(a, e, changes *ExternalLoadBalancerAttachment) error {
if a == nil {
if e.LoadBalancerName == "" {
return fi.RequiredField("LoadBalancerName")
}
if e.AutoscalingGroup == nil {
return fi.RequiredField("AutoscalingGroup")
}
}
return nil
}
func (_ *ExternalLoadBalancerAttachment) RenderAWS(t *awsup.AWSAPITarget, a, e, changes *ExternalLoadBalancerAttachment) error {
if e.LoadBalancerName == "" {
return fi.RequiredField("LoadBalancerName")
}
request := &autoscaling.AttachLoadBalancersInput{}
request.AutoScalingGroupName = e.AutoscalingGroup.Name
request.LoadBalancerNames = aws.StringSlice([]string{e.LoadBalancerName})
klog.V(2).Infof("Attaching autoscaling group %q to ELB %q", fi.StringValue(e.AutoscalingGroup.Name), e.LoadBalancerName)
_, err := t.Cloud.Autoscaling().AttachLoadBalancers(request)
if err != nil {
return fmt.Errorf("error attaching autoscaling group to ELB: %v", err)
}
return nil
}
type terraformExternalLoadBalancerAttachment struct {
ELB *terraform.Literal `json:"elb" cty:"elb"`
AutoscalingGroup *terraform.Literal `json:"autoscaling_group_name,omitempty" cty:"autoscaling_group_name"`
}
func (_ *ExternalLoadBalancerAttachment) RenderTerraform(t *terraform.TerraformTarget, a, e, changes *ExternalLoadBalancerAttachment) error {
tf := &terraformExternalLoadBalancerAttachment{
ELB: terraform.LiteralFromStringValue(e.LoadBalancerName),
AutoscalingGroup: e.AutoscalingGroup.TerraformLink(),
}
return t.RenderResource("aws_autoscaling_attachment", *e.Name, tf)
}
func (e *ExternalLoadBalancerAttachment) TerraformLink() *terraform.Literal {
return terraform.LiteralProperty("aws_autoscaling_attachment", e.LoadBalancerName, "id")
}
func (_ *ExternalLoadBalancerAttachment) RenderCloudformation(t *cloudformation.CloudformationTarget, a, e, changes *ExternalLoadBalancerAttachment) error {
cfObj, ok := t.Find(e.AutoscalingGroup.CloudformationLink())
if !ok {
// topo-sort fail?
return fmt.Errorf("AutoScalingGroup not yet rendered")
}
cf, ok := cfObj.(*cloudformationAutoscalingGroup)
if !ok {
return fmt.Errorf("unexpected type for CF record: %T", cfObj)
}
cf.LoadBalancerNames = append(cf.LoadBalancerNames, cloudformation.LiteralString(e.LoadBalancerName))
return nil
}

View File

@ -1,146 +0,0 @@
/*
Copyright 2019 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package awstasks
import (
"fmt"
"regexp"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/autoscaling"
"k8s.io/klog/v2"
"k8s.io/kops/upup/pkg/fi"
"k8s.io/kops/upup/pkg/fi/cloudup/awsup"
"k8s.io/kops/upup/pkg/fi/cloudup/cloudformation"
"k8s.io/kops/upup/pkg/fi/cloudup/terraform"
)
// +kops:fitask
type ExternalTargetGroupAttachment struct {
Name *string
Lifecycle *fi.Lifecycle
TargetGroupARN string
AutoscalingGroup *AutoscalingGroup
}
func (e *ExternalTargetGroupAttachment) name() string {
re := regexp.MustCompile("arn:.*:targetgroup/")
return re.ReplaceAllString(*e.Name, "")
}
func (e *ExternalTargetGroupAttachment) Find(c *fi.Context) (*ExternalTargetGroupAttachment, error) {
cloud := c.Cloud.(awsup.AWSCloud)
if e.TargetGroupARN == "" {
return nil, fmt.Errorf("InstanceGroup did not have TargetGroupARNs set")
}
g, err := findAutoscalingGroup(cloud, *e.AutoscalingGroup.Name)
if err != nil {
return nil, err
}
if g == nil {
return nil, nil
}
for _, name := range g.TargetGroupARNs {
if aws.StringValue(name) != e.TargetGroupARN {
continue
}
actual := &ExternalTargetGroupAttachment{}
actual.TargetGroupARN = e.TargetGroupARN
actual.AutoscalingGroup = e.AutoscalingGroup
// Prevent spurious changes
actual.Name = e.Name // ELB attachments don't have tags
actual.Lifecycle = e.Lifecycle
return actual, nil
}
return nil, nil
}
func (e *ExternalTargetGroupAttachment) Run(c *fi.Context) error {
return fi.DefaultDeltaRunMethod(e, c)
}
func (s *ExternalTargetGroupAttachment) CheckChanges(a, e, changes *ExternalTargetGroupAttachment) error {
if a == nil {
if e.TargetGroupARN == "" {
return fi.RequiredField("TargetGroupARN")
}
if e.AutoscalingGroup == nil {
return fi.RequiredField("AutoscalingGroup")
}
}
return nil
}
func (_ *ExternalTargetGroupAttachment) RenderAWS(t *awsup.AWSAPITarget, a, e, changes *ExternalTargetGroupAttachment) error {
if e.TargetGroupARN == "" {
return fi.RequiredField("TargetGroupARN")
}
request := &autoscaling.AttachLoadBalancerTargetGroupsInput{}
request.AutoScalingGroupName = e.AutoscalingGroup.Name
request.TargetGroupARNs = aws.StringSlice([]string{e.TargetGroupARN})
klog.V(2).Infof("Attaching autoscaling group %q to Target Group %q", fi.StringValue(e.AutoscalingGroup.Name), e.TargetGroupARN)
_, err := t.Cloud.Autoscaling().AttachLoadBalancerTargetGroups(request)
if err != nil {
return fmt.Errorf("error attaching autoscaling group to ELB: %v", err)
}
return nil
}
type terraformExternalTargetGroupAttachment struct {
TargetGroupARN *terraform.Literal `json:"alb_target_group_arn,omitempty" cty:"alb_target_group_arn"`
AutoscalingGroup *terraform.Literal `json:"autoscaling_group_name,omitempty" cty:"autoscaling_group_name"`
}
func (_ *ExternalTargetGroupAttachment) RenderTerraform(t *terraform.TerraformTarget, a, e, changes *ExternalTargetGroupAttachment) error {
tf := &terraformExternalTargetGroupAttachment{
TargetGroupARN: terraform.LiteralFromStringValue(e.TargetGroupARN),
AutoscalingGroup: e.AutoscalingGroup.TerraformLink(),
}
return t.RenderResource("aws_autoscaling_attachment", e.name(), tf)
}
func (e *ExternalTargetGroupAttachment) TerraformLink() *terraform.Literal {
return terraform.LiteralProperty("aws_autoscaling_attachment", e.name(), "id")
}
func (_ *ExternalTargetGroupAttachment) RenderCloudformation(t *cloudformation.CloudformationTarget, a, e, changes *ExternalTargetGroupAttachment) error {
cfObj, ok := t.Find(e.AutoscalingGroup.CloudformationLink())
if !ok {
// topo-sort fail?
return fmt.Errorf("AutoScalingGroup not yet rendered")
}
cf, ok := cfObj.(*cloudformationAutoscalingGroup)
if !ok {
return fmt.Errorf("unexpected type for CF record: %T", cfObj)
}
cf.TargetGroupARNs = append(cf.TargetGroupARNs, cloudformation.LiteralString(e.TargetGroupARN))
return nil
}

View File

@ -1,51 +0,0 @@
// +build !ignore_autogenerated
/*
Copyright 2020 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Code generated by fitask. DO NOT EDIT.
package awstasks
import (
"k8s.io/kops/upup/pkg/fi"
)
// ExternalLoadBalancerAttachment
var _ fi.HasLifecycle = &ExternalLoadBalancerAttachment{}
// GetLifecycle returns the Lifecycle of the object, implementing fi.HasLifecycle
func (o *ExternalLoadBalancerAttachment) GetLifecycle() *fi.Lifecycle {
return o.Lifecycle
}
// SetLifecycle sets the Lifecycle of the object, implementing fi.SetLifecycle
func (o *ExternalLoadBalancerAttachment) SetLifecycle(lifecycle fi.Lifecycle) {
o.Lifecycle = &lifecycle
}
var _ fi.HasName = &ExternalLoadBalancerAttachment{}
// GetName returns the Name of the object, implementing fi.HasName
func (o *ExternalLoadBalancerAttachment) GetName() *string {
return o.Name
}
// String is the stringer function for the task, producing readable output using fi.TaskAsString
func (o *ExternalLoadBalancerAttachment) String() string {
return fi.TaskAsString(o)
}

View File

@ -67,6 +67,9 @@ type LoadBalancer struct {
Tags map[string]string
ForAPIServer bool
// Shared is set if this is an external LB (one we don't create or own)
Shared *bool
}
var _ fi.CompareWithID = &LoadBalancer{}
@ -438,6 +441,13 @@ func (e *LoadBalancer) Run(c *fi.Context) error {
return fi.DefaultDeltaRunMethod(e, c)
}
func (_ *LoadBalancer) ShouldCreate(a, e, changes *LoadBalancer) (bool, error) {
if fi.BoolValue(e.Shared) {
return false, nil
}
return true, nil
}
func (e *LoadBalancer) Normalize() {
// We need to sort our arrays consistently, so we don't get spurious changes
sort.Stable(OrderSubnetsById(e.Subnets))
@ -449,11 +459,15 @@ func (s *LoadBalancer) CheckChanges(a, e, changes *LoadBalancer) error {
if fi.StringValue(e.Name) == "" {
return fi.RequiredField("Name")
}
if len(e.SecurityGroups) == 0 {
return fi.RequiredField("SecurityGroups")
}
if len(e.Subnets) == 0 {
return fi.RequiredField("Subnets")
shared := fi.BoolValue(e.Shared)
if !shared {
if len(e.SecurityGroups) == 0 {
return fi.RequiredField("SecurityGroups")
}
if len(e.Subnets) == 0 {
return fi.RequiredField("Subnets")
}
}
if e.AccessLog != nil {
@ -483,6 +497,11 @@ func (s *LoadBalancer) CheckChanges(a, e, changes *LoadBalancer) error {
}
func (_ *LoadBalancer) RenderAWS(t *awsup.AWSAPITarget, a, e, changes *LoadBalancer) error {
shared := fi.BoolValue(e.Shared)
if shared {
return nil
}
var loadBalancerName string
if a == nil {
if e.LoadBalancerName == nil {
@ -692,6 +711,11 @@ type terraformLoadBalancerHealthCheck struct {
}
func (_ *LoadBalancer) RenderTerraform(t *terraform.TerraformTarget, a, e, changes *LoadBalancer) error {
shared := fi.BoolValue(e.Shared)
if shared {
return nil
}
cloud := t.Cloud.(awsup.AWSCloud)
if e.LoadBalancerName == nil {
@ -782,6 +806,16 @@ func (_ *LoadBalancer) RenderTerraform(t *terraform.TerraformTarget, a, e, chang
}
func (e *LoadBalancer) TerraformLink(params ...string) *terraform.Literal {
shared := fi.BoolValue(e.Shared)
if shared {
if e.Name == nil {
klog.Fatalf("Name must be set, if LB is shared: %s", e)
}
klog.V(4).Infof("reusing existing LB with name %q", *e.Name)
return terraform.LiteralFromStringValue(*e.Name)
}
prop := "id"
if len(params) > 0 {
prop = params[0]
@ -836,6 +870,11 @@ func (_ *LoadBalancer) RenderCloudformation(t *cloudformation.CloudformationTarg
// If this resource has a public IP address and is also in a VPC that is defined in the same template,
// you must use the DependsOn attribute to declare a dependency on the VPC-gateway attachment.
shared := fi.BoolValue(e.Shared)
if shared {
return nil
}
cloud := t.Cloud.(awsup.AWSCloud)
if e.LoadBalancerName == nil {
@ -912,6 +951,16 @@ func (_ *LoadBalancer) RenderCloudformation(t *cloudformation.CloudformationTarg
}
func (e *LoadBalancer) CloudformationLink() *cloudformation.Literal {
shared := fi.BoolValue(e.Shared)
if shared {
if e.Name == nil {
klog.Fatalf("Name must be set, if LB is shared: %s", e)
}
klog.V(4).Infof("reusing existing LB with name %q", *e.Name)
return cloudformation.LiteralString(*e.Name)
}
return cloudformation.Ref("AWS::ElasticLoadBalancing::LoadBalancer", *e.Name)
}

View File

@ -0,0 +1,144 @@
/*
Copyright 2020 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package awstasks
import (
"fmt"
"k8s.io/klog/v2"
"k8s.io/kops/upup/pkg/fi"
"k8s.io/kops/upup/pkg/fi/cloudup/awsup"
"k8s.io/kops/upup/pkg/fi/cloudup/cloudformation"
"k8s.io/kops/upup/pkg/fi/cloudup/terraform"
)
// TargetGroup manages an targetgroup used for an ALB/NLB.
// +kops:fitask
type TargetGroup struct {
Name *string
Lifecycle *fi.Lifecycle
// ARN is the Amazon Resource Name for the Target Group
ARN *string
// Shared is set if this is an external LB (one we don't create or own)
Shared *bool
}
var _ fi.CompareWithID = &TargetGroup{}
func (e *TargetGroup) CompareWithID() *string {
return e.ARN
}
func (e *TargetGroup) Find(c *fi.Context) (*TargetGroup, error) {
if e.ARN == nil {
return nil, fmt.Errorf("ARN must be set for TargetGroup")
}
actual := &TargetGroup{}
actual.ARN = e.ARN
// Prevent spurious changes
actual.Name = e.Name
actual.Lifecycle = e.Lifecycle
return actual, nil
}
func (e *TargetGroup) Run(c *fi.Context) error {
return fi.DefaultDeltaRunMethod(e, c)
}
func (_ *TargetGroup) ShouldCreate(a, e, changes *TargetGroup) (bool, error) {
if fi.BoolValue(e.Shared) {
return false, nil
}
return true, nil
}
func (s *TargetGroup) CheckChanges(a, e, changes *TargetGroup) error {
if a == nil {
if e.ARN == nil {
return fi.RequiredField("ARN")
}
}
return nil
}
func (_ *TargetGroup) RenderAWS(t *awsup.AWSAPITarget, a, e, changes *TargetGroup) error {
shared := fi.BoolValue(e.Shared)
if shared {
return nil
}
return fmt.Errorf("non shared Target Groups is not yet supported")
}
type terraformTargetGroup struct {
Name *string `json:"name" cty:"name"`
}
func (_ *TargetGroup) RenderTerraform(t *terraform.TerraformTarget, a, e, changes *TargetGroup) error {
shared := fi.BoolValue(e.Shared)
if shared {
return nil
}
return fmt.Errorf("non shared Target Groups is not yet supported")
}
func (e *TargetGroup) TerraformLink(params ...string) *terraform.Literal {
shared := fi.BoolValue(e.Shared)
if shared {
if e.ARN == nil {
klog.Fatalf("ARN must be set for shared Target Group: %s", e)
}
klog.V(4).Infof("reusing existing Target Group with ARN %q", *e.ARN)
return terraform.LiteralFromStringValue(*e.ARN)
}
return nil
}
type cloudformationTargetGroup struct {
Name *string `json:"Name,omitempty"`
}
func (_ *TargetGroup) RenderCloudformation(t *cloudformation.CloudformationTarget, a, e, changes *TargetGroup) error {
shared := fi.BoolValue(e.Shared)
if shared {
return nil
}
return fmt.Errorf("non shared Target Groups is not yet supported")
}
func (e *TargetGroup) CloudformationLink() *cloudformation.Literal {
shared := fi.BoolValue(e.Shared)
if shared {
if e.ARN == nil {
klog.Fatalf("ARN must be set for shared Target Group: %s", e)
}
klog.V(4).Infof("reusing existing Target Group with ARN %q", *e.ARN)
return cloudformation.LiteralString(*e.ARN)
}
return nil
}

View File

@ -24,28 +24,28 @@ import (
"k8s.io/kops/upup/pkg/fi"
)
// ExternalTargetGroupAttachment
// TargetGroup
var _ fi.HasLifecycle = &ExternalTargetGroupAttachment{}
var _ fi.HasLifecycle = &TargetGroup{}
// GetLifecycle returns the Lifecycle of the object, implementing fi.HasLifecycle
func (o *ExternalTargetGroupAttachment) GetLifecycle() *fi.Lifecycle {
func (o *TargetGroup) GetLifecycle() *fi.Lifecycle {
return o.Lifecycle
}
// SetLifecycle sets the Lifecycle of the object, implementing fi.SetLifecycle
func (o *ExternalTargetGroupAttachment) SetLifecycle(lifecycle fi.Lifecycle) {
func (o *TargetGroup) SetLifecycle(lifecycle fi.Lifecycle) {
o.Lifecycle = &lifecycle
}
var _ fi.HasName = &ExternalTargetGroupAttachment{}
var _ fi.HasName = &TargetGroup{}
// GetName returns the Name of the object, implementing fi.HasName
func (o *ExternalTargetGroupAttachment) GetName() *string {
func (o *TargetGroup) GetName() *string {
return o.Name
}
// String is the stringer function for the task, producing readable output using fi.TaskAsString
func (o *ExternalTargetGroupAttachment) String() string {
func (o *TargetGroup) String() string {
return fi.TaskAsString(o)
}