-
Notifications
You must be signed in to change notification settings - Fork 200
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
introduce upgrade mechanism and add upgrade patch to v1 storage migra…
…tion Signed-off-by: Jeeva Kandasamy <[email protected]>
- Loading branch information
Showing
23 changed files
with
2,050 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
pkg/reconciler/shared/tektonconfig/upgrade/helper/storage_version_migrator.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
Copyright 2023 The Tekton 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 upgrade | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"go.uber.org/zap" | ||
apierrs "k8s.io/apimachinery/pkg/api/errors" | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
"knative.dev/pkg/apiextensions/storageversion" | ||
) | ||
|
||
// performs crd storage version upgrade | ||
// lists all the resources and, | ||
// keeps only one storage version on the crd | ||
func MigrateStorageVersion(ctx context.Context, logger *zap.SugaredLogger, migrator *storageversion.Migrator, crdGroups []string) error { | ||
logger.Infof("migrating %d group resources", len(crdGroups)) | ||
|
||
for _, crdGroupString := range crdGroups { | ||
crdGroup := schema.ParseGroupResource(crdGroupString) | ||
if crdGroup.Empty() { | ||
logger.Errorf("unable to parse group version: %s", crdGroupString) | ||
return fmt.Errorf("unable to parse group version: %s", crdGroupString) | ||
} | ||
logger.Info("migrating group resource ", crdGroup) | ||
if err := migrator.Migrate(ctx, crdGroup); err != nil { | ||
if apierrs.IsNotFound(err) { | ||
logger.Infow("ignoring resource migration - unable to fetch a crd", | ||
"crd", crdGroup, err, | ||
) | ||
continue | ||
} | ||
logger.Errorw("failed to migrate: ", err) | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} |
103 changes: 103 additions & 0 deletions
103
pkg/reconciler/shared/tektonconfig/upgrade/helper/storage_version_migrator_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/* | ||
Copyright 2023 The Tekton 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 upgrade | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
apix "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" | ||
apixFake "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset/fake" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
runtime "k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
dynamicFake "k8s.io/client-go/dynamic/fake" | ||
"knative.dev/pkg/apiextensions/storageversion" | ||
"knative.dev/pkg/logging" | ||
) | ||
|
||
func TestMigrateStorageVersion(t *testing.T) { | ||
fakeKind := schema.GroupKind{ | ||
Kind: "Fake", | ||
Group: "group.dev", | ||
} | ||
|
||
fakeGroup := schema.GroupResource{ | ||
Resource: "fakes", | ||
Group: "group.dev", | ||
} | ||
|
||
fakeCRD := &apix.CustomResourceDefinition{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: fakeGroup.String(), | ||
}, | ||
Spec: apix.CustomResourceDefinitionSpec{ | ||
Group: fakeKind.Group, | ||
Versions: []apix.CustomResourceDefinitionVersion{ | ||
{Name: "v1alpha1", Served: true, Storage: false}, | ||
{Name: "v1beta1", Served: true, Storage: false}, | ||
{Name: "v1", Served: true, Storage: true}, | ||
}, | ||
}, | ||
Status: apix.CustomResourceDefinitionStatus{ | ||
StoredVersions: []string{ | ||
"v1alpha1", | ||
"v1beta1", | ||
"v1", | ||
}, | ||
}, | ||
} | ||
|
||
ctx := context.TODO() | ||
resources := []runtime.Object{ | ||
fakeCrdResource("resource-1", "group.dev/v1alpha1"), | ||
fakeCrdResource("resource-2", "group.dev/v1beta1"), | ||
fakeCrdResource("resource-3", "group.dev/v1beta2"), | ||
fakeCrdResource("resource-4", "group.dev/v1"), | ||
} | ||
|
||
dclient := dynamicFake.NewSimpleDynamicClient(runtime.NewScheme(), resources...) | ||
cclient := apixFake.NewSimpleClientset(fakeCRD) | ||
migrator := storageversion.NewMigrator(dclient, cclient) | ||
logger := logging.FromContext(ctx) | ||
|
||
// TEST | ||
// expects only "v1" | ||
err := MigrateStorageVersion(ctx, logger, migrator, []string{"fakes.group.dev", "unknown.group.dev"}) | ||
assert.NoError(t, err) | ||
crd, err := cclient.ApiextensionsV1().CustomResourceDefinitions().Get(ctx, fakeCRD.GetName(), metav1.GetOptions{}) | ||
assert.NoError(t, err) | ||
storageVersions := crd.Status.StoredVersions | ||
assert.Len(t, storageVersions, 1) | ||
assert.Equal(t, "v1", storageVersions[0]) | ||
|
||
} | ||
|
||
func fakeCrdResource(name, apiVersion string) runtime.Object { | ||
return &unstructured.Unstructured{ | ||
Object: map[string]interface{}{ | ||
"apiVersion": apiVersion, | ||
"kind": "Fake", | ||
"metadata": map[string]interface{}{ | ||
"name": name, | ||
"namespace": "default", | ||
}, | ||
}, | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
pkg/reconciler/shared/tektonconfig/upgrade/post_upgrade.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
Copyright 2023 The Tekton 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 upgrade | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/tektoncd/operator/pkg/client/clientset/versioned" | ||
upgrade "github.com/tektoncd/operator/pkg/reconciler/shared/tektonconfig/upgrade/helper" | ||
"go.uber.org/zap" | ||
apixclient "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset" | ||
"k8s.io/client-go/dynamic" | ||
"k8s.io/client-go/kubernetes" | ||
"k8s.io/client-go/rest" | ||
"knative.dev/pkg/apiextensions/storageversion" | ||
) | ||
|
||
// performs storage versions upgrade | ||
// lists all the resources and keeps only one storage version | ||
func upgradeStorageVersion(ctx context.Context, logger *zap.SugaredLogger, k8sClient kubernetes.Interface, operatorClient versioned.Interface, restConfig *rest.Config) error { | ||
// resources to be upgraded | ||
crdGroups := []string{ | ||
"clusterinterceptors.triggers.tekton.dev", | ||
"clustertasks.tekton.dev", | ||
"clustertriggerbindings.triggers.tekton.dev", | ||
"customruns.tekton.dev", | ||
"eventlisteners.triggers.tekton.dev", | ||
"extensions.dashboard.tekton.dev", | ||
"interceptors.triggers.tekton.dev", | ||
"pipelineruns.tekton.dev", | ||
"pipelines.tekton.dev", | ||
"resolutionrequests.resolution.tekton.dev", | ||
"taskruns.tekton.dev", | ||
"tasks.tekton.dev", | ||
"triggerbindings.triggers.tekton.dev", | ||
"triggers.triggers.tekton.dev", | ||
"triggertemplates.triggers.tekton.dev", | ||
"verificationpolicies.tekton.dev", | ||
} | ||
|
||
migrator := storageversion.NewMigrator( | ||
dynamic.NewForConfigOrDie(restConfig), | ||
apixclient.NewForConfigOrDie(restConfig), | ||
) | ||
|
||
return upgrade.MigrateStorageVersion(ctx, logger, migrator, crdGroups) | ||
} |
Oops, something went wrong.