Skip to content

Commit

Permalink
Validate whether spec.version matches the version of manifests (knati…
Browse files Browse the repository at this point in the history
…ve#246)

* Valid whether spec.version matches the version of manifests

* Refactor the test cases of TestTargetManifest
  • Loading branch information
Vincent authored Aug 17, 2020
1 parent 130790d commit 5c3535d
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 4 deletions.
54 changes: 50 additions & 4 deletions pkg/reconciler/common/releases.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,18 @@ var cache = map[string]mf.Manifest{}
func TargetVersion(instance v1alpha1.KComponent) string {
target := instance.GetSpec().GetVersion()
if target == "" {
// If spec.version is not set and spec.manifests are set, we leave the target version empty.
if len(instance.GetSpec().GetManifests()) != 0 {
return ""
}
return latestRelease(instance)
}
return target
}

// TargetManifest returns the manifest for the TargetVersion
func TargetManifest(instance v1alpha1.KComponent) (mf.Manifest, error) {
return fetch(manifestPath(TargetVersion(instance), instance))
return versionValidation(TargetVersion(instance), instance)
}

// InstalledManifest returns the version currently installed, which is
Expand All @@ -64,10 +68,10 @@ func TargetManifest(instance v1alpha1.KComponent) (mf.Manifest, error) {
// So we return the target manifest if status.version is empty.
func InstalledManifest(instance v1alpha1.KComponent) (mf.Manifest, error) {
current := instance.GetStatus().GetVersion()
if current != "" {
return fetch(installedManifestPath(current, instance))
if len(instance.GetStatus().GetManifests()) == 0 && current == "" {
return TargetManifest(instance)
}
return TargetManifest(instance)
return fetch(installedManifestPath(current, instance))
}

// IsUpDowngradeEligible returns the bool indicate whether the installed manifest is able to upgrade or downgrade to
Expand Down Expand Up @@ -108,6 +112,48 @@ func IsUpDowngradeEligible(instance v1alpha1.KComponent) bool {
return false
}

func getVersionKey(instance v1alpha1.KComponent) string {
switch instance.(type) {
case *v1alpha1.KnativeServing:
return "serving.knative.dev/release"
case *v1alpha1.KnativeEventing:
return "eventing.knative.dev/release"
}
return ""
}

func versionValidation(version string, instance v1alpha1.KComponent) (mf.Manifest, error) {
manifestsPath := componentURL(version, instance)
if manifestsPath == "" {
// The spec.manifests are empty. There is no need to check whether the versions match.
return fetch(manifestPath(version, instance))
}

manifests, err := fetch(manifestsPath)
if err != nil {
// If we cannot access the manifests, there is no need to check whether the versions match.
return manifests, err
}

if version == "" {
// If target version is empty, there is no need to check whether the versions match.
return manifests, nil
}

targetVersion := sanitizeSemver(version)
key := getVersionKey(instance)
for _, u := range manifests.Resources() {
// Check the labels of the resources one by one to see if the version matches the target version.
manifestVersion := u.GetLabels()[key]
if targetVersion != manifestVersion && manifestVersion != "" {
return mf.Manifest{}, fmt.Errorf("The version of the manifests %s does not match the target "+
"version of the operator CR %s. The resource name is %s.", manifestVersion, targetVersion, u.GetName())
}
}

return manifests, nil
}

func abs(x int) int {
if x < 0 {
return -x
Expand Down
67 changes: 67 additions & 0 deletions pkg/reconciler/common/releases_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package common

import (
"fmt"
"os"
"testing"

Expand Down Expand Up @@ -279,3 +280,69 @@ func TestIsUpDowngradeEligible(t *testing.T) {
})
}
}

func TestTargetManifest(t *testing.T) {
tests := []struct {
name string
component v1alpha1.KComponent
expectedError error
}{{
name: "knative-serving with spec.manifests matched",
component: &v1alpha1.KnativeServing{
Spec: v1alpha1.KnativeServingSpec{
CommonSpec: v1alpha1.CommonSpec{
Version: "0.16.0",
Manifests: []v1alpha1.Manifest{{
Url: "testdata/kodata/knative-serving/${version}/serving-core.yaml",
}, {
Url: "testdata/kodata/knative-serving/${version}/serving-hpa.yaml",
}},
},
},
},
expectedError: nil,
}, {
name: "knative-serving with spec.manifests unmatched",
component: &v1alpha1.KnativeServing{
Spec: v1alpha1.KnativeServingSpec{
CommonSpec: v1alpha1.CommonSpec{
Version: "0.16.0",
Manifests: []v1alpha1.Manifest{{
Url: "testdata/invalid_kodata/knative-serving/${version}_unmatched_version/serving-core.yaml",
}, {
Url: "testdata/invalid_kodata/knative-serving/${version}_unmatched_version/serving-hpa.yaml",
}},
},
},
},
expectedError: fmt.Errorf("The version of the manifests %s does not match the target "+
"version of the operator CR %s. The resource name is %s.", "v0.16.2", "v0.16.0", "knative-serving"),
}, {
name: "knative-serving with spec.manifests matched but no spec.version",
component: &v1alpha1.KnativeServing{
Spec: v1alpha1.KnativeServingSpec{
CommonSpec: v1alpha1.CommonSpec{
Manifests: []v1alpha1.Manifest{{
Url: "testdata/kodata/knative-serving/0.16.0/serving-core.yaml",
}, {
Url: "testdata/kodata/knative-serving/0.16.0/serving-hpa.yaml",
}},
},
},
},
expectedError: nil,
}}

koPath := "testdata/kodata"
os.Setenv(KoEnvKey, koPath)
defer os.Unsetenv(KoEnvKey)

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
_, err := TargetManifest(test.component)
if err != test.expectedError {
util.AssertEqual(t, err.Error(), test.expectedError.Error())
}
})
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Copyright 2020 The Knative 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
#
# https://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.

apiVersion: v1
kind: Namespace
metadata:
name: knative-serving
labels:
serving.knative.dev/release: "v0.16.2"
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Copyright 2020 The Knative 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
#
# https://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.

apiVersion: v1
kind: Namespace
metadata:
name: knative-serving
labels:
serving.knative.dev/release: "v0.16.2"

0 comments on commit 5c3535d

Please sign in to comment.