Skip to content

Commit

Permalink
Merge pull request #407 from weaveworks/fix-363
Browse files Browse the repository at this point in the history
Move status fields from ClusterConfig to ClusterStatus
  • Loading branch information
errordeveloper authored Jan 8, 2019
2 parents 494b483 + 0c81e85 commit 8794482
Show file tree
Hide file tree
Showing 12 changed files with 84 additions and 73 deletions.
14 changes: 9 additions & 5 deletions pkg/apis/eksctl.io/v1alpha3/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,14 @@ type ClusterMeta struct {
Tags map[string]string `json:"tags,omitempty"`
}

// ClusterStatus hold read-only attributes of a cluster
type ClusterStatus struct {
Endpoint string `json:"endpoint,omitempty"`
CertificateAuthorityData []byte `json:"certificateAuthorityData,omitempty"`
ARN string `json:"arn,omitempty"`
StackName string `json:"stackName,omitempty"`
}

// String returns canonical representation of ClusterMeta
func (c *ClusterMeta) String() string {
return fmt.Sprintf("%s.%s.eksctl.io", c.Name, c.Region)
Expand Down Expand Up @@ -161,11 +169,7 @@ type ClusterConfig struct {
// +optional
AvailabilityZones []string `json:"availabilityZones,omitempty"`

// TODO: move under status
Endpoint string
CertificateAuthorityData []byte
ARN string
ClusterStackName string
Status *ClusterStatus `json:"status,omitempty"`
}

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
Expand Down
29 changes: 25 additions & 4 deletions pkg/apis/eksctl.io/v1alpha3/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

72 changes: 27 additions & 45 deletions pkg/cfn/builder/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,13 @@ func newStackWithOutputs(outputs map[string]string) cfn.Stack {
}

var _ = Describe("CloudFormation template builder API", func() {
var caCertData []byte

Describe("should decode CA data", func() {
var err error
caCertData, err = base64.StdEncoding.DecodeString(caCert)
It("should not error", func() { Expect(err).ShouldNot(HaveOccurred()) })
})

testAZs := []string{"us-west-2b", "us-west-2a", "us-west-2c"}

Expand All @@ -197,6 +204,12 @@ var _ = Describe("CloudFormation template builder API", func() {

cfg.Metadata.Region = "us-west-2"
cfg.Metadata.Name = clusterName

cfg.Status = &api.ClusterStatus{
CertificateAuthorityData: caCertData,
Endpoint: endpoint,
}

cfg.AvailabilityZones = testAZs
ng.Name = "ng-abcd1234"
ng.InstanceType = "t2.medium"
Expand Down Expand Up @@ -253,21 +266,20 @@ var _ = Describe("CloudFormation template builder API", func() {
}

Describe("GetAllOutputsFromClusterStack", func() {
caCertData, err := base64.StdEncoding.DecodeString(caCert)
It("should not error", func() { Expect(err).ShouldNot(HaveOccurred()) })

expected := &api.ClusterConfig{
TypeMeta: api.ClusterConfigTypeMeta(),
Metadata: &api.ClusterMeta{
Region: "us-west-2",
Name: clusterName,
Version: "1.11",
},
Endpoint: endpoint,
CertificateAuthorityData: caCertData,
ARN: arn,
AvailabilityZones: testAZs,
VPC: testVPC(),
Status: &api.ClusterStatus{
Endpoint: endpoint,
CertificateAuthorityData: caCertData,
ARN: arn,
},
AvailabilityZones: testAZs,
VPC: testVPC(),
NodeGroups: []*api.NodeGroup{
{
AMI: "",
Expand Down Expand Up @@ -331,7 +343,6 @@ var _ = Describe("CloudFormation template builder API", func() {

Describe("AutoNameTag", func() {
cfg, ng := newClusterConfigAndNodegroup()
cfg.CertificateAuthorityData = []byte("MyCA")

rs := NewNodeGroupResourceSet(cfg, "eksctl-test-123-cluster", ng)

Expand Down Expand Up @@ -371,12 +382,8 @@ var _ = Describe("CloudFormation template builder API", func() {
})

Describe("NodeGroupTags", func() {
cfg := api.NewClusterConfig()
ng := cfg.NewNodeGroup()
cfg, ng := newClusterConfigAndNodegroup()

cfg.Metadata.Region = "us-west-2"
cfg.Metadata.Name = clusterName
cfg.AvailabilityZones = testAZs
ng.InstanceType = "t2.medium"
ng.Name = "ng-abcd1234"

Expand Down Expand Up @@ -410,7 +417,6 @@ var _ = Describe("CloudFormation template builder API", func() {

Describe("NodeGroupAutoScaling", func() {
cfg, ng := newClusterConfigAndNodegroup()
cfg.CertificateAuthorityData = []byte("MyCA")

ng.IAM.WithAddonPolicies.AutoScaler = true

Expand Down Expand Up @@ -448,13 +454,8 @@ var _ = Describe("CloudFormation template builder API", func() {
})

Describe("NodeGroup{PrivateNetworking=true AllowSSH=true}", func() {
cfg := api.NewClusterConfig()
ng := cfg.NewNodeGroup()
cfg, ng := newClusterConfigAndNodegroup()

cfg.Metadata.Region = "us-west-2"
cfg.Metadata.Name = clusterName
cfg.AvailabilityZones = testAZs
cfg.CertificateAuthorityData = []byte("MyCA")
ng.AllowSSH = true
ng.InstanceType = "t2.medium"
ng.PrivateNetworking = true
Expand Down Expand Up @@ -510,13 +511,8 @@ var _ = Describe("CloudFormation template builder API", func() {
})

Describe("NodeGroup{PrivateNetworking=false AllowSSH=true}", func() {
cfg := api.NewClusterConfig()
ng := cfg.NewNodeGroup()
cfg, ng := newClusterConfigAndNodegroup()

cfg.Metadata.Region = "us-west-2"
cfg.Metadata.Name = clusterName
cfg.AvailabilityZones = testAZs
cfg.CertificateAuthorityData = []byte("MyCA")
ng.AllowSSH = true
ng.InstanceType = "t2.medium"
ng.PrivateNetworking = false
Expand Down Expand Up @@ -575,12 +571,8 @@ var _ = Describe("CloudFormation template builder API", func() {
})

Describe("NodeGroup{PrivateNetworking=false AllowSSH=false}", func() {
cfg := api.NewClusterConfig()
ng := cfg.NewNodeGroup()
cfg, ng := newClusterConfigAndNodegroup()

cfg.Metadata.Region = "us-west-2"
cfg.Metadata.Name = clusterName
cfg.AvailabilityZones = testAZs
cfg.VPC = &api.ClusterVPC{
Network: api.Network{
ID: vpcID,
Expand Down Expand Up @@ -611,7 +603,7 @@ var _ = Describe("CloudFormation template builder API", func() {
},
},
}
cfg.CertificateAuthorityData = []byte("MyCA")

ng.AvailabilityZones = []string{testAZs[1]}
ng.AllowSSH = false
ng.InstanceType = "t2.medium"
Expand Down Expand Up @@ -701,15 +693,10 @@ var _ = Describe("CloudFormation template builder API", func() {

var c *cloudconfig.CloudConfig

caCertData, err := base64.StdEncoding.DecodeString(caCert)
It("should not error", func() { Expect(err).ShouldNot(HaveOccurred()) })

cfg.Endpoint = endpoint
cfg.CertificateAuthorityData = caCertData
cfg.NodeGroups[0].InstanceType = "m5.large"

rs := NewNodeGroupResourceSet(cfg, "eksctl-test-123-cluster", ng)
err = rs.AddAllResources()
err := rs.AddAllResources()
It("should add all resources without errors", func() {
Expect(err).ShouldNot(HaveOccurred())
})
Expand Down Expand Up @@ -769,17 +756,12 @@ var _ = Describe("CloudFormation template builder API", func() {

var c *cloudconfig.CloudConfig

caCertData, err := base64.StdEncoding.DecodeString(caCert)
It("should not error", func() { Expect(err).ShouldNot(HaveOccurred()) })

cfg.Endpoint = endpoint
cfg.CertificateAuthorityData = caCertData
cfg.VPC.CIDR, _ = ipnet.ParseCIDR("10.1.0.0/16")
cfg.NodeGroups[0].AMIFamily = "Ubuntu1804"
cfg.NodeGroups[0].InstanceType = "m5.large"

rs := NewNodeGroupResourceSet(cfg, "eksctl-test-123-cluster", ng)
err = rs.AddAllResources()
err := rs.AddAllResources()
It("should add all resources without errors", func() {
Expect(err).ShouldNot(HaveOccurred())
})
Expand Down
10 changes: 6 additions & 4 deletions pkg/cfn/builder/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,13 @@ func (c *ClusterResourceSet) GetAllOutputs(stack cfn.Stack) error {
if err != nil {
return errors.Wrap(err, "decoding certificate authority data")
}
c.spec.CertificateAuthorityData = caData

c.spec.Endpoint = c.outputs[cfnOutputClusterEndpoint]
c.spec.ARN = c.outputs[cfnOutputClusterARN]
c.spec.ClusterStackName = c.outputs[cfnOutputClusterStackName]
c.spec.Status = &api.ClusterStatus{
CertificateAuthorityData: caData,
Endpoint: c.outputs[cfnOutputClusterEndpoint],
ARN: c.outputs[cfnOutputClusterARN],
StackName: c.outputs[cfnOutputClusterStackName],
}

return nil
}
2 changes: 1 addition & 1 deletion pkg/cfn/manager/nodegroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func (c *StackCollection) BlockingWaitDeleteNodeGroup(name string) error {
// ScaleNodeGroup will scale an existing nodegroup
func (c *StackCollection) ScaleNodeGroup(ng *api.NodeGroup) error {
clusterName := c.makeClusterStackName()
c.spec.ClusterStackName = clusterName
c.spec.Status = &api.ClusterStatus{StackName: clusterName}
name := c.MakeNodeGroupStackName(ng.Name)
logger.Info("scaling nodegroup stack %q in cluster %s", name, clusterName)

Expand Down
4 changes: 4 additions & 0 deletions pkg/ctl/create/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,10 @@ func doCreateCluster(p *api.ProviderConfig, cfg *api.ClusterConfig, nameArg stri
}
meta.Name = utils.ClusterName(meta.Name, nameArg)

if cfg.Status != nil {
return fmt.Errorf("status fields are read-only")
}

subnetsGiven = len(*subnets[api.SubnetTopologyPrivate])+len(*subnets[api.SubnetTopologyPublic]) != 0

if withoutNodeGroup {
Expand Down
8 changes: 5 additions & 3 deletions pkg/eks/eks.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,16 @@ func (c *ClusterProvider) GetCredentials(spec *api.ClusterConfig) error {
}
logger.Debug("cluster = %#v", cluster)

spec.Endpoint = *cluster.Endpoint

data, err := base64.StdEncoding.DecodeString(*cluster.CertificateAuthority.Data)
if err != nil {
return errors.Wrap(err, "decoding certificate authority data")
}

spec.CertificateAuthorityData = data
spec.Status = &api.ClusterStatus{
Endpoint: *cluster.Endpoint,
CertificateAuthorityData: data,
}

return nil
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/nodebootstrap/userdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func makeMetadata(spec *api.ClusterConfig) []string {
return []string{
fmt.Sprintf("AWS_DEFAULT_REGION=%s", spec.Metadata.Region),
fmt.Sprintf("AWS_EKS_CLUSTER_NAME=%s", spec.Metadata.Name),
fmt.Sprintf("AWS_EKS_ENDPOINT=%s", spec.Endpoint),
fmt.Sprintf("AWS_EKS_ENDPOINT=%s", spec.Status.Endpoint),
}
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/nodebootstrap/userdata_al2.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func makeAmazonLinux2Config(spec *api.ClusterConfig, ng *api.NodeGroup) (configF
return nil, err
}

if spec.CertificateAuthorityData == nil || len(spec.CertificateAuthorityData) == 0 {
if spec.Status.CertificateAuthorityData == nil || len(spec.Status.CertificateAuthorityData) == 0 {
return nil, errors.New("invalid cluster config: missing CertificateAuthorityData")
}

Expand All @@ -28,7 +28,7 @@ func makeAmazonLinux2Config(spec *api.ClusterConfig, ng *api.NodeGroup) (configF
"kubelet.env": {content: strings.Join(makeKubeletParamsCommon(spec, ng), "\n")},
// TODO: https://github.com/weaveworks/eksctl/issues/161
"kubelet-config.json": {isAsset: true},
"ca.crt": {content: string(spec.CertificateAuthorityData)},
"ca.crt": {content: string(spec.Status.CertificateAuthorityData)},
"kubeconfig.yaml": {content: string(clientConfigData)},
},
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/nodebootstrap/userdata_ubuntu.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func makeUbuntu1804Config(spec *api.ClusterConfig, ng *api.NodeGroup) (configFil
"metadata.env": {content: strings.Join(makeMetadata(spec), "\n")},
"kubelet.env": {content: strings.Join(makeKubeletParamsCommon(spec, ng), "\n")},
// TODO: https://github.com/weaveworks/eksctl/issues/161
"ca.crt": {content: string(spec.CertificateAuthorityData)},
"ca.crt": {content: string(spec.Status.CertificateAuthorityData)},
"kubeconfig.yaml": {content: string(clientConfigData)},
},
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/utils/kubeconfig/kubeconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func New(spec *api.ClusterConfig, username, certificateAuthorityPath string) (*c
c := &clientcmdapi.Config{
Clusters: map[string]*clientcmdapi.Cluster{
clusterName: {
Server: spec.Endpoint,
Server: spec.Status.Endpoint,
},
},
Contexts: map[string]*clientcmdapi.Context{
Expand All @@ -51,7 +51,7 @@ func New(spec *api.ClusterConfig, username, certificateAuthorityPath string) (*c
}

if certificateAuthorityPath == "" {
c.Clusters[clusterName].CertificateAuthorityData = spec.CertificateAuthorityData
c.Clusters[clusterName].CertificateAuthorityData = spec.Status.CertificateAuthorityData
} else {
c.Clusters[clusterName].CertificateAuthority = certificateAuthorityPath
}
Expand Down
6 changes: 1 addition & 5 deletions pkg/utils/kubeconfig/kubeconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,7 @@ var _ = Describe("Kubeconfig", func() {
},
SecurityGroup: "",
},
Endpoint: "",
CertificateAuthorityData: []uint8(nil),
ARN: "",
ClusterStackName: "",
AvailabilityZones: []string{"us-west-2b", "us-west-2a", "us-west-2c"},
AvailabilityZones: []string{"us-west-2b", "us-west-2a", "us-west-2c"},
}

var (
Expand Down

0 comments on commit 8794482

Please sign in to comment.