Repository: kubean-io/kubean
Pull request #1096 (2024-01-25 06:40:25)

migrate old version localArtifactSet
<!--  

Thanks for sending a pull request!  Here are some tips for you:

* make sure your commit is signed off

-->

#### What type of PR is this?

/kind bug

#### What this PR does / why we need it:
 Migrate old versions of LocalArtifactSet without a release version label.

#### Which issue(s) this PR fixes:
<!--
*Automatically closes linked issue when PR is merged.
Usage: `Fixes #<issue number>`, or `Fixes (paste link of issue)`.
_If PR is about `failing-tests or flakes`, please post the related issues/tests in a comment and do not use `Fixes`_*
-->
Fixes #

#### Special notes for your reviewer:



Patch:
diff --git a/charts/kubean/templates/manifest.cr.yaml b/charts/kubean/templates/manifest.cr.yaml
index 78445f4eb..449d629c4 100644
--- a/charts/kubean/templates/manifest.cr.yaml
+++ b/charts/kubean/templates/manifest.cr.yaml
@@ -1,15 +1,15 @@
 apiVersion: "kubean.io/v1alpha1"
 kind: "Manifest"
 metadata:
-  name: "manifest-2.22-d8a8fb0"
+  name: "manifest-2.22-774d824"
   labels:
     kubean.io/sprayRelease: "2.22"
   annotations:
-    kubean.io/sprayTimestamp: "1705940622"
+    kubean.io/sprayTimestamp: "1704981596"
     kubean.io/sprayRelease: "2.22"
-    kubean.io/sprayCommit: "d8a8fb0"
+    kubean.io/sprayCommit: "774d824"
 spec:
-  kubesprayVersion: "d8a8fb03a21704d53b437c28d1325bc5853a6437"
+  kubesprayVersion: "774d824d0bbe5cd52cf6524e37bda9ca226aaa80"
   kubeanVersion: ""
   docker:
     - os: "redhat-7"
diff --git a/docs/overrides/releases/artifacts.md b/docs/overrides/releases/artifacts.md
index 24c2fe776..b813b9e51 100644
--- a/docs/overrides/releases/artifacts.md
+++ b/docs/overrides/releases/artifacts.md
@@ -11,21 +11,6 @@
     </tr>
   </thead>
   
-  <tbody>
-      <tr>
-        <td rowspan=3> 📅 2024-01-22 16:13:31 </td>
-        <td rowspan=1>
-           📝 <code><a href="https://raw.githubusercontent.com/kubean-io/kubean-manifest/main/manifests/manifest-2.21-e86c0cf.yml">manifest-2.21-e86c0cf.yml</a></code>
-        </td>
-      </tr>
-      <tr>
-        <td rowspan=1> 📦 <code>ghcr.io/kubean-io/spray-job:2.21-e86c0cf</code> </td>
-      </tr>
-      <tr>
-        <td rowspan=1> 📦 <code>ghcr.io/kubean-io/airgap-patch:2.21-e86c0cf</code> </td>
-      </tr>
-  </tbody>
-  
   <tbody>
       <tr>
         <td rowspan=3> 📅 2024-01-12 07:28:51 </td>
@@ -54,21 +39,6 @@
     </tr>
   </thead>
   
-  <tbody>
-      <tr>
-        <td rowspan=3> 📅 2024-01-22 16:23:42 </td>
-        <td rowspan=1>
-           📝 <code><a href="https://raw.githubusercontent.com/kubean-io/kubean-manifest/main/manifests/manifest-2.22-d8a8fb0.yml">manifest-2.22-d8a8fb0.yml</a></code>
-        </td>
-      </tr>
-      <tr>
-        <td rowspan=1> 📦 <code>ghcr.io/kubean-io/spray-job:2.22-d8a8fb0</code> </td>
-      </tr>
-      <tr>
-        <td rowspan=1> 📦 <code>ghcr.io/kubean-io/airgap-patch:2.22-d8a8fb0</code> </td>
-      </tr>
-  </tbody>
-  
   <tbody>
       <tr>
         <td rowspan=3> 📅 2024-01-11 13:59:56 </td>
diff --git a/pkg/controllers/offlineversion/controller.go b/pkg/controllers/offlineversion/controller.go
index 28a6ec741..23524d114 100644
--- a/pkg/controllers/offlineversion/controller.go
+++ b/pkg/controllers/offlineversion/controller.go
@@ -77,6 +77,19 @@ func (c *Controller) Reconcile(ctx context.Context, req controllerruntime.Reques
 	sprayRelease, ok := localartifactset.ObjectMeta.Labels[constants.KeySprayRelease]
 	if !ok {
 		klog.Warningf("No label %s found in %s", constants.KeySprayRelease, localartifactset.Name)
+
+		// Migrate old versions of LocalArtifactSet without a release version label.
+		sprayRelease = "master"
+		if localartifactset.ObjectMeta.Labels != nil {
+			localartifactset.ObjectMeta.Labels[constants.KeySprayRelease] = sprayRelease
+		} else {
+			localartifactset.ObjectMeta.Labels = map[string]string{constants.KeySprayRelease: sprayRelease}
+		}
+		_, err := c.LocalArtifactSetClientSet.KubeanV1alpha1().LocalArtifactSets().Update(context.Background(), localartifactset, metav1.UpdateOptions{})
+		if err != nil {
+			klog.Error(err)
+			return controllerruntime.Result{RequeueAfter: Loop}, nil
+		}
 		return controllerruntime.Result{}, nil
 	}
 


Test patch:
diff --git a/pkg/controllers/offlineversion/controller_test.go b/pkg/controllers/offlineversion/controller_test.go
index a2d19f306..708180999 100644
--- a/pkg/controllers/offlineversion/controller_test.go
+++ b/pkg/controllers/offlineversion/controller_test.go
@@ -489,7 +489,7 @@ func TestReconcile(t *testing.T) {
 			want: true,
 		},
 		{
-			name: "ComponentsversionGlobal not exist",
+			name: "localArtifact missing release label",
 			args: func() bool {
 				controller := &Controller{
 					Client:                    newFakeClient(),
@@ -511,11 +511,20 @@ func TestReconcile(t *testing.T) {
 					},
 				}
 
+				manifest := &manifestv1alpha1.Manifest{
+					ObjectMeta: metav1.ObjectMeta{
+						Name: "manifest-master",
+					},
+				}
+				infomanifest.GetVersionedManifest().Op("add", manifest, nil)
+				controller.InfoManifestClientSet.KubeanV1alpha1().Manifests().Create(context.Background(), manifest, metav1.CreateOptions{})
 				controller.Client.Create(context.Background(), &offlineVersionData)
 				controller.LocalArtifactSetClientSet.KubeanV1alpha1().LocalArtifactSets().Create(context.Background(), &offlineVersionData, metav1.CreateOptions{})
 
 				result, _ := controller.Reconcile(context.Background(), controllerruntime.Request{NamespacedName: types.NamespacedName{Name: offlineVersionData.Name}})
-				return result.RequeueAfter == 0 && result.Requeue == false
+				localartifactset, _ := controller.LocalArtifactSetClientSet.KubeanV1alpha1().LocalArtifactSets().Get(context.Background(), offlineVersionData.Name, metav1.GetOptions{})
+				release, ok := localartifactset.ObjectMeta.Labels[constants.KeySprayRelease]
+				return ok == true && release == "master" && result.Requeue == false && result.RequeueAfter == 0
 			},
 			want: true,
 		},