Skip to content
This repository has been archived by the owner on Oct 10, 2023. It is now read-only.

Commit

Permalink
Update osInfo annotation during cluster upgrade (#3092)
Browse files Browse the repository at this point in the history
- This changes updates osInfo annotation on the cluster resource during
  cluster upgrade

Signed-off-by: Anuj Chaudhari <anujc@vmware.com>
  • Loading branch information
anujc25 authored Aug 8, 2022
1 parent a0ff565 commit 5003fae
Show file tree
Hide file tree
Showing 4 changed files with 192 additions and 0 deletions.
16 changes: 16 additions & 0 deletions pkg/v1/tkg/client/upgrade_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ type ComponentInfo struct {
CAPDImageRepo string
AwsRegionToAMIMap map[string][]tkgconfigbom.AMIInfo
AzureImage tkgconfigbom.AzureInfo
OsInfo tkgconfigbom.OSInfo
}

type upgradeStatus string
Expand Down Expand Up @@ -327,6 +328,13 @@ func (c *TkgClient) DoClusterUpgrade(regionalClusterClient clusterclient.Client,
return err
}

// osInfo annotation format: "ubuntu,20.04,amd64"
osInfoString := fmt.Sprintf("%s,%s,%s", upgradeClusterConfig.UpgradeComponentInfo.OsInfo.Name, upgradeClusterConfig.UpgradeComponentInfo.OsInfo.Version, upgradeClusterConfig.UpgradeComponentInfo.OsInfo.Arch)
err = regionalClusterClient.PatchClusterObjectAnnotations(upgradeClusterConfig.ClusterName, upgradeClusterConfig.ClusterNamespace, "osInfo", osInfoString)
if err != nil {
return errors.Wrap(err, "error while patching osInfo to the cluster resource")
}

return nil
}

Expand Down Expand Up @@ -527,6 +535,7 @@ func (c *TkgClient) getUpgradeClusterConfig(options *UpgradeClusterOptions) (*Cl
if err == nil && azureVMImage != nil {
// TODO: what if error is returned or azureVMImage is nil, handle that case
upgradeInfo.UpgradeComponentInfo.AzureImage = *azureVMImage
upgradeInfo.UpgradeComponentInfo.OsInfo = azureVMImage.OSInfo
}

// We are hard-coding the assumption that during upgrade imageConfig.ImageRepository should take precedence
Expand Down Expand Up @@ -1074,6 +1083,12 @@ func (c *TkgClient) createVsphereInfrastructureTemplateForUpgrade(regionalCluste
return errors.Wrap(err, "unable to get/verify vsphere template")
}

clusterUpgradeConfig.UpgradeComponentInfo.OsInfo = tkgconfigbom.OSInfo{
Name: vSphereVM.DistroName,
Version: vSphereVM.DistroVersion,
Arch: vSphereVM.DistroArch,
}

clusterUpgradeConfig.UpgradeComponentInfo.VSphereVMTemplateName = vSphereVM.Name
clusterUpgradeConfig.UpgradeComponentInfo.VSphereVMTemplateMOID = vSphereVM.Moid

Expand Down Expand Up @@ -1308,6 +1323,7 @@ func (c *TkgClient) getAWSAMIIDForK8sVersion(regionalClusterClient clusterclient
return errors.Errorf("unable to find the AMI ID for AWSTemplate for region %s and kubernetes version %s, with the provided os option", awsClusterObject.Spec.Region, upgradeInfo.UpgradeComponentInfo.KubernetesVersion)
}
upgradeInfo.UpgradeComponentInfo.AwsAMIID = selectedAMI.ID
upgradeInfo.UpgradeComponentInfo.OsInfo = selectedAMI.OSInfo
}

if upgradeInfo.UpgradeComponentInfo.AwsAMIID == "" {
Expand Down
11 changes: 11 additions & 0 deletions pkg/v1/tkg/clusterclient/clusterclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,8 @@ type Client interface {
PatchClusterWithOperationStartedStatus(clusterName, namespace, operationType string, timeout time.Duration) error
// PatchClusterObjectWithTKGVersion applies patch to cluster objects based on given tkgVersion string
PatchClusterObjectWithTKGVersion(clusterName, clusterNamespace, tkgVersion string) error
// PatchClusterObjectAnnotations applies patch to cluster objects to update annotation with specified key:value
PatchClusterObjectAnnotations(clusterName, namespace, key, value string) error
// GetManagementClusterTKGVersion returns the TKG version of a management cluster based on the
// annotation value present in cluster object
GetManagementClusterTKGVersion(mgmtClusterName, clusterNamespace string) (string, error)
Expand Down Expand Up @@ -1050,6 +1052,15 @@ func (c *client) PatchClusterObjectWithTKGVersion(clusterName, namespace, tkgVer
return nil
}

func (c *client) PatchClusterObjectAnnotations(clusterName, namespace, key, value string) error {
patchAnnotations := fmt.Sprintf(annotationPatchFormat, key, value)
err := c.PatchClusterObject(clusterName, namespace, patchAnnotations)
if err != nil {
return errors.Wrapf(err, "unable to patch the cluster object with %v", patchAnnotations)
}
return nil
}

func (c *client) GetManagementClusterTKGVersion(mgmtClusterName, clusterNamespace string) (string, error) {
mcObject := &capiv1alpha3.Cluster{}
err := c.GetResource(mcObject, mgmtClusterName, clusterNamespace, nil, nil)
Expand Down
85 changes: 85 additions & 0 deletions pkg/v1/tkg/clusterclient/clusterclient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2851,6 +2851,91 @@ prod.repo.com: prod.custom.repo.com`
})
})

Describe("Unit tests for PatchClusterObjectAnnotations", func() {
var (
fakeClientSet crtclient.Client
resources []runtime.Object
key, value string
)
fakecluster1 := &capi.Cluster{
ObjectMeta: metav1.ObjectMeta{
Name: "fake-cluster-1",
Namespace: "fake-namespace",
Annotations: map[string]string{
"key-foo": "value-foo",
"key-bar": "value-bar",
},
},
}

JustBeforeEach(func() {
reInitialize()

fakeClientSet = fake.NewClientBuilder().WithScheme(scheme).WithRuntimeObjects(resources...).Build()
crtClientFactory.NewClientReturns(fakeClientSet, nil)

clusterClientOptions = NewOptions(poller, crtClientFactory, discoveryClientFactory, nil)
kubeConfigPath := getConfigFilePath("config1.yaml")
clstClient, err = NewClient(kubeConfigPath, "", clusterClientOptions)
Expect(err).NotTo(HaveOccurred())

err = clstClient.PatchClusterObjectAnnotations("fake-cluster-1", "fake-namespace", key, value)
})

Context("When matching cluster not found", func() {
BeforeEach(func() {
resources = []runtime.Object{}
})
It("should return error", func() {
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("unable to patch the cluster object with"))
})
})

Context("When cluster object is found and matching annotation doesn't exists", func() {
BeforeEach(func() {
resources = []runtime.Object{fakecluster1}
key = "key-fake"
value = "value-fake"
})
It("should not return error", func() {
Expect(err).NotTo(HaveOccurred())
})
It("should add new annotation", func() {
cluster := &capi.Cluster{}
err = clstClient.GetResource(cluster, "fake-cluster-1", "fake-namespace", nil, nil)
Expect(err).NotTo(HaveOccurred())
annotations := cluster.GetAnnotations()
Expect(annotations).To(HaveKey("key-bar"))
Expect(annotations).To(HaveKey("key-foo"))
Expect(annotations).To(HaveKey("key-fake"))
Expect(annotations["key-bar"]).To(Equal("value-bar"))
Expect(annotations["key-foo"]).To(Equal("value-foo"))
Expect(annotations["key-fake"]).To(Equal("value-fake"))
})
})

Context("When cluster object is found and matching annotation exists", func() {
BeforeEach(func() {
resources = []runtime.Object{fakecluster1}
key = "key-bar"
value = "value-bar-updated"
})
It("should not return error", func() {
Expect(err).NotTo(HaveOccurred())
})
It("should update existing annotation", func() {
cluster := &capi.Cluster{}
err = clstClient.GetResource(cluster, "fake-cluster-1", "fake-namespace", nil, nil)
Expect(err).NotTo(HaveOccurred())
annotations := cluster.GetAnnotations()
Expect(annotations).To(HaveKey("key-bar"))
Expect(annotations).To(HaveKey("key-foo"))
Expect(annotations["key-bar"]).To(Equal("value-bar-updated"))
Expect(annotations["key-foo"]).To(Equal("value-foo"))
})
})
})
})

func createTempDirectory() {
Expand Down
80 changes: 80 additions & 0 deletions pkg/v1/tkg/fakes/clusterclient.go

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

0 comments on commit 5003fae

Please sign in to comment.