Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 51 additions & 1 deletion pkg/reconciler/common/releases.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ var cache = map[string]mf.Manifest{}
// version known to the operator is returned.
func TargetVersion(instance v1alpha1.KComponent) string {
version := instance.GetSpec().GetVersion()
if strings.EqualFold(version, LATEST_VERSION) {
return getLatestRelease(instance, version)
}

if len(instance.GetSpec().GetManifests()) == 0 {
if version == "" {
return latestRelease(instance)
Expand Down Expand Up @@ -262,6 +266,11 @@ func componentDir(instance v1alpha1.KComponent) string {
return ""
}

func componentIngressDir() string {
koDataDir := os.Getenv(KoEnvKey)
return filepath.Join(koDataDir, "ingress")
}

func additionalManifestPath(instance v1alpha1.KComponent) string {
// Create the comma-separated string for URLs in spec.additionalManifests
addManifests := instance.GetSpec().GetAdditionalManifests()
Expand Down Expand Up @@ -324,11 +333,25 @@ func SanitizeSemver(version string) string {
return fmt.Sprintf("v%s", version)
}

// allIngressReleases returns the all the available release versions
// available under kodata directory for Knative component.
func allIngressReleases() ([]string, error) {
// List all the directories available under kodata
pathname := componentIngressDir()
return allReleasesUnderPath(pathname)
}

// allReleases returns the all the available release versions
// available under kodata directory for Knative component.
func allReleases(instance v1alpha1.KComponent) ([]string, error) {
// List all the directories available under kodata
pathname := componentDir(instance)
return allReleasesUnderPath(pathname)
}

// allComponentReleases returns the all the available release versions
// available under kodata directory for a certain path.
func allReleasesUnderPath(pathname string) ([]string, error) {
fileList, err := ioutil.ReadDir(pathname)
if err != nil {
return nil, err
Expand All @@ -346,7 +369,7 @@ func allReleases(instance v1alpha1.KComponent) ([]string, error) {
}
}
if len(releaseTags) == 0 {
return nil, fmt.Errorf("unable to find any version number for %v", instance)
return nil, fmt.Errorf("unable to find any version number under the path %v", pathname)
}

// This function makes sure the versions are sorted in a descending order.
Expand All @@ -363,6 +386,17 @@ func latestRelease(instance v1alpha1.KComponent) string {
return getLatestRelease(instance, "")
}

// GetLatestIngressRelease returns the latest release tag available under kodata directory for the ingress
// based on spec.version.
func GetLatestIngressRelease(version string) string {
// The versions are in a descending order, so the first one will be the latest version.
vers, err := allIngressReleases()
if err != nil {
panic(err)
}
return getLatestReleaseFromList(vers, version)
}

// getLatestRelease returns the latest release tag available under kodata directory for Knative component
// based on spec.version.
func getLatestRelease(instance v1alpha1.KComponent, version string) string {
Expand All @@ -371,11 +405,27 @@ func getLatestRelease(instance v1alpha1.KComponent, version string) string {
if err != nil {
panic(err)
}
return getLatestReleaseFromList(vers, version)
}

// getLatestReleaseFromList returns the latest release tag available under kodata directory for Knative component
// based on spec.version.
func getLatestReleaseFromList(vers []string, version string) string {
if version == "" {
return vers[0]
}

if strings.EqualFold(version, LATEST_VERSION) {
// If spec.version is set to latest, look up if the directory latest is available.
// If not, return the newest available version instead.
for _, val := range vers {
if val == version {
return val
}
}
return vers[0]
}

for _, val := range vers {
if strings.HasPrefix(val, version) &&
semver.MajorMinor(SanitizeSemver(val)) == semver.MajorMinor(SanitizeSemver(version)) {
Expand Down
29 changes: 29 additions & 0 deletions pkg/reconciler/common/releases_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,35 @@ func TestTargetVersion(t *testing.T) {
}
}

func TestTargetVersionNoLatestDir(t *testing.T) {
koPath := "testdata/kodata-no-latest"

tests := []struct {
name string
component v1alpha1.KComponent
expected string
}{{
name: "serving CR with the version latest",
component: &v1alpha1.KnativeServing{
Spec: v1alpha1.KnativeServingSpec{
CommonSpec: v1alpha1.CommonSpec{
Version: "latest",
},
},
},
expected: "0.16.1",
}}

os.Setenv(KoEnvKey, koPath)
defer os.Unsetenv(KoEnvKey)
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
version := TargetVersion(test.component)
util.AssertEqual(t, version, test.expected)
})
}
}

func TestGetLatestRelease(t *testing.T) {
koPath := "testdata/kodata"

Expand Down
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.0"
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.0"
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-core
labels:
serving.knative.dev/release: "v0.16.1"
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-crd
labels:
serving.knative.dev/release: "v0.16.1"
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-hpa
labels:
serving.knative.dev/release: "v0.16.1"
9 changes: 8 additions & 1 deletion pkg/reconciler/knativeserving/ingress/ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"os"
"path/filepath"
"strings"

mf "github.com/manifestival/manifestival"
"golang.org/x/mod/semver"
Expand Down Expand Up @@ -92,7 +93,13 @@ func getIngress(version string, manifest *mf.Manifest) error {
}
koDataDir := os.Getenv(common.KoEnvKey)
// Ingresses are saved in the directory named major.minor. We remove the patch number.
ingressVersion := semver.MajorMinor(common.SanitizeSemver(version))[1:]
ingressVersion := common.LATEST_VERSION
if !strings.EqualFold(version, common.LATEST_VERSION) {
ingressVersion = semver.MajorMinor(common.SanitizeSemver(version))[1:]
}

// This line can make sure a valid available ingress version is returned.
ingressVersion = common.GetLatestIngressRelease(ingressVersion)
ingressPath := filepath.Join(koDataDir, "ingress", ingressVersion)
m, err := common.FetchManifest(ingressPath)
if err != nil {
Expand Down
58 changes: 58 additions & 0 deletions pkg/reconciler/knativeserving/ingress/ingress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,64 @@ func TestAppendInstalledIngresses(t *testing.T) {
}
}

func TestAppendTargetIngresses(t *testing.T) {
os.Setenv(common.KoEnvKey, "testdata/kodata")
defer os.Unsetenv(common.KoEnvKey)

tests := []struct {
name string
instance servingv1alpha1.KnativeServing
expectedIngressPath string
expectedErr error
}{{
name: "Available target ingresses",
instance: servingv1alpha1.KnativeServing{
Spec: servingv1alpha1.KnativeServingSpec{
CommonSpec: servingv1alpha1.CommonSpec{
Version: "0.21.0",
},
},
},
expectedIngressPath: os.Getenv(common.KoEnvKey) + "/ingress/0.21",
expectedErr: nil,
}, {
name: "Unavailable target ingresses",
instance: servingv1alpha1.KnativeServing{
Spec: servingv1alpha1.KnativeServingSpec{
CommonSpec: servingv1alpha1.CommonSpec{
Version: "0.12.1",
},
},
},
expectedErr: fmt.Errorf("stat testdata/kodata/ingress/0.12: no such file or directory"),
}, {
name: "Get the latest target ingresses when the directory latest is unavailable",
instance: servingv1alpha1.KnativeServing{
Spec: servingv1alpha1.KnativeServingSpec{
CommonSpec: servingv1alpha1.CommonSpec{
Version: "latest",
},
},
},
expectedIngressPath: os.Getenv(common.KoEnvKey) + "/ingress/0.22",
expectedErr: nil,
}}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
manifest, _ := mf.ManifestFrom(mf.Slice{})
err := AppendTargetIngresses(context.TODO(), &manifest, &tt.instance)
if err != nil {
util.AssertEqual(t, err.Error(), tt.expectedErr.Error())
util.AssertEqual(t, len(manifest.Resources()), 0)
} else {
util.AssertEqual(t, err, tt.expectedErr)
util.AssertEqual(t, util.DeepMatchWithPath(manifest, tt.expectedIngressPath), true)
}
})
}
}

func TestGetIngressWithFilters(t *testing.T) {
os.Setenv(common.KoEnvKey, "testdata/kodata")
defer os.Unsetenv(common.KoEnvKey)
Expand Down

This file was deleted.

Loading