Skip to content

Commit

Permalink
add eksa management version annotation to management cluster
Browse files Browse the repository at this point in the history
  • Loading branch information
cxbrowne1207 committed Jan 12, 2024
1 parent 18094e9 commit 7f33908
Show file tree
Hide file tree
Showing 14 changed files with 246 additions and 7 deletions.
9 changes: 8 additions & 1 deletion cmd/eksctl-anywhere/cmd/createcluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,14 @@ func (cc *createClusterOptions) createCluster(cmd *cobra.Command, _ []string) er
return err
}

clusterKubeconfig := kubeconfig.FromClusterName(clusterSpec.Cluster.Name)
client, err := deps.UnAuthKubeClient.BuildClientFromKubeconfig(clusterKubeconfig)
if err != nil {
return err
}

Check warning on line 212 in cmd/eksctl-anywhere/cmd/createcluster.go

View check run for this annotation

Codecov / codecov/patch

cmd/eksctl-anywhere/cmd/createcluster.go#L208-L212

Added lines #L208 - L212 were not covered by tests

createCluster := workflows.NewCreate(
client,

Check warning on line 215 in cmd/eksctl-anywhere/cmd/createcluster.go

View check run for this annotation

Codecov / codecov/patch

cmd/eksctl-anywhere/cmd/createcluster.go#L215

Added line #L215 was not covered by tests
deps.Bootstrapper,
deps.Provider,
deps.ClusterManager,
Expand All @@ -222,7 +229,7 @@ func (cc *createClusterOptions) createCluster(cmd *cobra.Command, _ []string) er
Spec: clusterSpec,
WorkloadCluster: &types.Cluster{
Name: clusterSpec.Cluster.Name,
KubeconfigFile: kubeconfig.FromClusterName(clusterSpec.Cluster.Name),
KubeconfigFile: clusterKubeconfig,

Check warning on line 232 in cmd/eksctl-anywhere/cmd/createcluster.go

View check run for this annotation

Codecov / codecov/patch

cmd/eksctl-anywhere/cmd/createcluster.go#L232

Added line #L232 was not covered by tests
},
ManagementCluster: mgmt,
Provider: deps.Provider,
Expand Down
5 changes: 5 additions & 0 deletions cmd/eksctl-anywhere/cmd/upgradecluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,13 @@ func (uc *upgradeClusterOptions) upgradeCluster(cmd *cobra.Command, args []strin

upgradeValidations := upgradevalidations.New(validationOpts)

client, err := deps.UnAuthKubeClient.BuildClientFromKubeconfig(workloadCluster.KubeconfigFile)
if err != nil {
return err
}

Check warning on line 207 in cmd/eksctl-anywhere/cmd/upgradecluster.go

View check run for this annotation

Codecov / codecov/patch

cmd/eksctl-anywhere/cmd/upgradecluster.go#L204-L207

Added lines #L204 - L207 were not covered by tests
if clusterConfig.IsSelfManaged() {
upgrade := management.NewUpgrade(
client,

Check warning on line 210 in cmd/eksctl-anywhere/cmd/upgradecluster.go

View check run for this annotation

Codecov / codecov/patch

cmd/eksctl-anywhere/cmd/upgradecluster.go#L210

Added line #L210 was not covered by tests
deps.Provider,
deps.CAPIManager,
deps.ClusterManager,
Expand Down
8 changes: 7 additions & 1 deletion cmd/eksctl-anywhere/cmd/upgrademanagementcomponents.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,13 @@ var upgradeManagementComponentsCmd = &cobra.Command{
}
defer close(cmd.Context(), deps)

clusterKubeconfig := kubeconfig.FromClusterName(clusterSpec.Cluster.Name)
client, err := deps.UnAuthKubeClient.BuildClientFromKubeconfig(clusterKubeconfig)
if err != nil {
return err
}

Check warning on line 77 in cmd/eksctl-anywhere/cmd/upgrademanagementcomponents.go

View check run for this annotation

Codecov / codecov/patch

cmd/eksctl-anywhere/cmd/upgrademanagementcomponents.go#L73-L77

Added lines #L73 - L77 were not covered by tests
runner := management.NewUpgradeManagementComponentsRunner(
client,

Check warning on line 79 in cmd/eksctl-anywhere/cmd/upgrademanagementcomponents.go

View check run for this annotation

Codecov / codecov/patch

cmd/eksctl-anywhere/cmd/upgrademanagementcomponents.go#L79

Added line #L79 was not covered by tests
deps.Provider,
deps.CAPIManager,
deps.ClusterManager,
Expand All @@ -82,7 +88,7 @@ var upgradeManagementComponentsCmd = &cobra.Command{

managementCluster := &types.Cluster{
Name: clusterSpec.Cluster.Name,
KubeconfigFile: kubeconfig.FromClusterName(clusterSpec.Cluster.Name),
KubeconfigFile: clusterKubeconfig,

Check warning on line 91 in cmd/eksctl-anywhere/cmd/upgrademanagementcomponents.go

View check run for this annotation

Codecov / codecov/patch

cmd/eksctl-anywhere/cmd/upgrademanagementcomponents.go#L91

Added line #L91 was not covered by tests
}

validator := management.NewUMCValidator(managementCluster, deps.Kubectl)
Expand Down
8 changes: 6 additions & 2 deletions internal/test/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,12 @@ func NewClusterSpec(opts ...ClusterSpecOpt) *cluster.Spec {
}
s.VersionsBundles = map[v1alpha1.KubernetesVersion]*cluster.VersionsBundle{
v1alpha1.Kube119: {
VersionsBundle: &releasev1alpha1.VersionsBundle{},
KubeDistro: &cluster.KubeDistro{},
VersionsBundle: &releasev1alpha1.VersionsBundle{
Eksa: releasev1alpha1.EksaBundle{
Version: "v0.0.0-dev+build.0000+000000",
},
},
KubeDistro: &cluster.KubeDistro{},
},
}
s.Bundles = &releasev1alpha1.Bundles{}
Expand Down
16 changes: 16 additions & 0 deletions pkg/api/v1alpha1/cluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ const (
// cluster object.
managementAnnotation = "anywhere.eks.amazonaws.com/managed-by"

// managementComponentsVersionAnnotation is an annotation applied to an EKS-A management cluster pointing to the current version of the management components.
// The value for this annotation is expected to correspond to an EKSARelease object version, following semantic version convention: e.g. v0.18.3
// This is an internal EKS-A managed annotation, not meant to be updated manually.
managementComponentsVersionAnnotation = "anywhere.eks.amazonaws.com/eksa-management-components-version"

// defaultEksaNamespace is the default namespace for EKS-A resources when not specified.
defaultEksaNamespace = "default"

Expand Down Expand Up @@ -1294,6 +1299,17 @@ func (c *Cluster) ControlPlaneAnnotation() string {
return controlPlaneAnnotation
}

// SetManagmentComponentsVersion sets the `eksa-management-components version` annotation on the Cluster object.
func (c *Cluster) SetManagmentComponentsVersion(version string) {
if c.IsManaged() {
return
}
if c.Annotations == nil {
c.Annotations = make(map[string]string, 1)
}
c.Annotations[managementComponentsVersionAnnotation] = version
}

// DisableControlPlaneIPCheck sets the `skip-ip-check` annotation on the Cluster object.
func (c *Cluster) DisableControlPlaneIPCheck() {
if c.Annotations == nil {
Expand Down
34 changes: 34 additions & 0 deletions pkg/api/v1alpha1/cluster_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3230,3 +3230,37 @@ func TestValidateCluster(t *testing.T) {
})
}
}

func TestCluster_SetManagmentComponentsVersion(t *testing.T) {
managmentClusterName := "mgmt-cluster"
expectedManagmentComponentVersion := "v0.0.0-dev+build.0000"

testCases := []struct {
name string
cluster *v1alpha1.Cluster
wantAnnotations map[string]string
}{
{
name: "self-managed cluster",
cluster: baseCluster(func(c *v1alpha1.Cluster) {
c.SetManagmentComponentsVersion(expectedManagmentComponentVersion)
}),
wantAnnotations: map[string]string{"anywhere.eks.amazonaws.com/eksa-management-components-version": expectedManagmentComponentVersion},
},
{
name: "managed cluster",
cluster: baseCluster(func(c *v1alpha1.Cluster) {
c.SetManagedBy(managmentClusterName)
c.SetManagmentComponentsVersion(expectedManagmentComponentVersion)
}),
wantAnnotations: map[string]string{"anywhere.eks.amazonaws.com/managed-by": managmentClusterName},
},
}

for _, tt := range testCases {
t.Run(tt.name, func(t *testing.T) {
g := NewWithT(t)
g.Expect(tt.cluster.Annotations).To(Equal(tt.wantAnnotations))
})
}
}
2 changes: 2 additions & 0 deletions pkg/task/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"sigs.k8s.io/yaml"

"github.com/aws/eks-anywhere/pkg/clients/kubernetes"
"github.com/aws/eks-anywhere/pkg/cluster"
"github.com/aws/eks-anywhere/pkg/filewriter"
"github.com/aws/eks-anywhere/pkg/logger"
Expand All @@ -27,6 +28,7 @@ type Task interface {

// Command context maintains the mutable and shared entities.
type CommandContext struct {
Client kubernetes.Client
Bootstrapper interfaces.Bootstrapper
Provider providers.Provider
ClusterManager interfaces.ClusterManager
Expand Down
22 changes: 21 additions & 1 deletion pkg/workflows/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"

"github.com/aws/eks-anywhere/pkg/clients/kubernetes"
"github.com/aws/eks-anywhere/pkg/cluster"
"github.com/aws/eks-anywhere/pkg/clustermarshaller"
"github.com/aws/eks-anywhere/pkg/constants"
Expand All @@ -16,7 +17,12 @@ import (
"github.com/aws/eks-anywhere/pkg/workflows/interfaces"
)

const (
eksaFieldManager = "eks-a-cli"
)

type Create struct {
client kubernetes.Client
bootstrapper interfaces.Bootstrapper
provider providers.Provider
clusterManager interfaces.ClusterManager
Expand All @@ -26,12 +32,14 @@ type Create struct {
packageInstaller interfaces.PackageInstaller
}

func NewCreate(bootstrapper interfaces.Bootstrapper, provider providers.Provider,
// NewCreate returns a Create instance.
func NewCreate(client kubernetes.Client, bootstrapper interfaces.Bootstrapper, provider providers.Provider,
clusterManager interfaces.ClusterManager, gitOpsManager interfaces.GitOpsManager,
writer filewriter.FileWriter, eksdInstaller interfaces.EksdInstaller,
packageInstaller interfaces.PackageInstaller,
) *Create {
return &Create{
client: client,
bootstrapper: bootstrapper,
provider: provider,
clusterManager: clusterManager,
Expand All @@ -51,6 +59,7 @@ func (c *Create) Run(ctx context.Context, clusterSpec *cluster.Spec, validator i
}
}
commandContext := &task.CommandContext{
Client: c.client,
Bootstrapper: c.bootstrapper,
Provider: c.provider,
ClusterManager: c.clusterManager,
Expand Down Expand Up @@ -372,6 +381,17 @@ func (s *InstallEksaComponentsTask) Run(ctx context.Context, commandContext *tas
commandContext.SetError(err)
return &CollectDiagnosticsTask{}
}

commandContext.ClusterSpec.Cluster.SetManagmentComponentsVersion(commandContext.ClusterSpec.EKSARelease.Spec.Version)
if err := commandContext.Client.ApplyServerSide(ctx,
eksaFieldManager,
commandContext.ClusterSpec.Cluster,
kubernetes.ApplyServerSideOptions{ForceOwnership: true},
); err != nil {
commandContext.SetError(err)
return &CollectDiagnosticsTask{}
}

err = commandContext.ClusterManager.ResumeEKSAControllerReconcile(ctx, targetCluster, commandContext.ClusterSpec, commandContext.Provider)
if err != nil {
commandContext.SetError(err)
Expand Down
64 changes: 63 additions & 1 deletion pkg/workflows/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"github.com/aws/eks-anywhere/internal/test"
"github.com/aws/eks-anywhere/pkg/api/v1alpha1"
"github.com/aws/eks-anywhere/pkg/bootstrapper"
"github.com/aws/eks-anywhere/pkg/clients/kubernetes"
clientmocks "github.com/aws/eks-anywhere/pkg/clients/kubernetes/mocks"
"github.com/aws/eks-anywhere/pkg/cluster"
writermocks "github.com/aws/eks-anywhere/pkg/filewriter/mocks"
"github.com/aws/eks-anywhere/pkg/providers"
Expand All @@ -23,6 +25,7 @@ import (

type createTestSetup struct {
t *testing.T
client *clientmocks.MockClient
packageInstaller *mocks.MockPackageInstaller
bootstrapper *mocks.MockBootstrapper
clusterManager *mocks.MockClusterManager
Expand All @@ -43,6 +46,7 @@ type createTestSetup struct {

func newCreateTest(t *testing.T) *createTestSetup {
mockCtrl := gomock.NewController(t)
client := clientmocks.NewMockClient(mockCtrl)
bootstrapper := mocks.NewMockBootstrapper(mockCtrl)
clusterManager := mocks.NewMockClusterManager(mockCtrl)
gitOpsManager := mocks.NewMockGitOpsManager(mockCtrl)
Expand All @@ -53,11 +57,12 @@ func newCreateTest(t *testing.T) *createTestSetup {

datacenterConfig := &v1alpha1.VSphereDatacenterConfig{}
machineConfigs := []providers.MachineConfig{&v1alpha1.VSphereMachineConfig{}}
workflow := workflows.NewCreate(bootstrapper, provider, clusterManager, gitOpsManager, writer, eksd, packageInstaller)
workflow := workflows.NewCreate(client, bootstrapper, provider, clusterManager, gitOpsManager, writer, eksd, packageInstaller)
validator := mocks.NewMockValidator(mockCtrl)

return &createTestSetup{
t: t,
client: client,
bootstrapper: bootstrapper,
clusterManager: clusterManager,
gitOpsManager: gitOpsManager,
Expand Down Expand Up @@ -202,6 +207,13 @@ func (c *createTestSetup) expectInstallEksaComponents() {
c.eksd.EXPECT().InstallEksdManifest(
c.ctx, c.clusterSpec, c.workloadCluster),

c.client.EXPECT().ApplyServerSide(
c.ctx,
"eks-a-cli",
c.clusterSpec.Cluster,
kubernetes.ApplyServerSideOptions{ForceOwnership: true},
),

c.clusterManager.EXPECT().ResumeEKSAControllerReconcile(c.ctx, c.workloadCluster, c.clusterSpec, c.provider),
)
}
Expand All @@ -224,6 +236,13 @@ func (c *createTestSetup) skipInstallEksaComponents() {
c.eksd.EXPECT().InstallEksdManifest(
c.ctx, c.clusterSpec, c.bootstrapCluster),

c.client.EXPECT().ApplyServerSide(
c.ctx,
"eks-a-cli",
c.clusterSpec.Cluster,
kubernetes.ApplyServerSideOptions{ForceOwnership: true},
),

c.clusterManager.EXPECT().ResumeEKSAControllerReconcile(c.ctx, c.bootstrapCluster, c.clusterSpec, c.provider),
)
}
Expand Down Expand Up @@ -293,6 +312,49 @@ func TestCreateRunSuccess(t *testing.T) {
}
}

func TestCreateRunInstallEksaComponentsApplyFail(t *testing.T) {
wantError := errors.New("test error")
test := newCreateTest(t)

test.expectSetup()
test.expectPreflightValidationsToPass()
test.expectCreateBootstrap()
test.expectCreateWorkload()
test.expectInstallResourcesOnManagementTask()
test.expectMoveManagement()
gomock.InOrder(
test.clusterManager.EXPECT().InstallCustomComponents(
test.ctx, test.clusterSpec, test.workloadCluster, test.provider),

test.eksd.EXPECT().InstallEksdCRDs(test.ctx, test.clusterSpec, test.workloadCluster),

test.provider.EXPECT().DatacenterConfig(test.clusterSpec).Return(test.datacenterConfig),

test.provider.EXPECT().MachineConfigs(test.clusterSpec).Return(test.machineConfigs),

test.clusterManager.EXPECT().CreateEKSAResources(
test.ctx, test.workloadCluster, test.clusterSpec, test.datacenterConfig, test.machineConfigs,
),

test.eksd.EXPECT().InstallEksdManifest(
test.ctx, test.clusterSpec, test.workloadCluster),

test.client.EXPECT().ApplyServerSide(
test.ctx,
"eks-a-cli",
test.clusterSpec.Cluster,
kubernetes.ApplyServerSideOptions{ForceOwnership: true},
).Return(wantError),
)
test.clusterManager.EXPECT().SaveLogsManagementCluster(test.ctx, test.clusterSpec, test.bootstrapCluster)
test.clusterManager.EXPECT().SaveLogsWorkloadCluster(test.ctx, test.provider, test.clusterSpec, test.workloadCluster)
test.writer.EXPECT().Write(fmt.Sprintf("%s-checkpoint.yaml", test.clusterSpec.Cluster.Name), gomock.Any())

if err := test.run(); err == nil {
t.Fatalf("Create.Run() err = %v, want err = %v", err, wantError)
}
}

func TestCreateRunAWSIamConfigFail(t *testing.T) {
wantError := errors.New("test error")
test := newCreateTest(t)
Expand Down
14 changes: 14 additions & 0 deletions pkg/workflows/management/core_components.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@ package management
import (
"context"

"github.com/aws/eks-anywhere/pkg/clients/kubernetes"
"github.com/aws/eks-anywhere/pkg/logger"
"github.com/aws/eks-anywhere/pkg/task"
"github.com/aws/eks-anywhere/pkg/types"
"github.com/aws/eks-anywhere/pkg/workflows"
)

const (
eksaFieldManager = "eks-a-cli"
)

type ensureEtcdCAPIComponentsExist struct{}

// Run ensureEtcdCAPIComponentsExist ensures ETCD CAPI providers on the management cluster.
Expand Down Expand Up @@ -85,6 +90,15 @@ func runUpgradeCoreComponents(ctx context.Context, commandContext *task.CommandC
}
commandContext.UpgradeChangeDiff.Append(changeDiff)

commandContext.ClusterSpec.Cluster.SetManagmentComponentsVersion(commandContext.ClusterSpec.EKSARelease.Spec.Version)
if err := commandContext.Client.ApplyServerSide(ctx,
eksaFieldManager,
commandContext.ClusterSpec.Cluster,
kubernetes.ApplyServerSideOptions{ForceOwnership: true},
); err != nil {
commandContext.SetError(err)
return err
}
return nil
}

Expand Down
Loading

0 comments on commit 7f33908

Please sign in to comment.