Skip to content

Commit

Permalink
make default channel optional
Browse files Browse the repository at this point in the history
This commit leaves default channel blank if not specified or is not contained in channels. This affects the `annotations.yaml` and make it "" for default channels. This will not affect index.db as the code to choose a channel to be the default still lives there. It should remain there since OLM uses that value.
  • Loading branch information
bowenislandsong committed May 5, 2020
1 parent 45043d4 commit 95f23b2
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ metadata:
tectonic-visibility: ocs
alm-examples: '[{"apiVersion":"monitoring.coreos.com/v1","kind":"Prometheus","metadata":{"name":"example","labels":{"prometheus":"k8s"}},"spec":{"replicas":2,"version":"v1.7.0","serviceAccountName":"prometheus-k8s","serviceMonitorSelector":{"matchExpressions":[{"key":"k8s-app","operator":"Exists"}]},"ruleSelector":{"matchLabels":{"role":"prometheus-rulefiles","prometheus":"k8s"}},"resources":{"requests":{"memory":"400Mi"}},"alerting":{"alertmanagers":[{"namespace":"monitoring","name":"alertmanager-main","port":"web"}]}}},{"apiVersion":"monitoring.coreos.com/v1","kind":"ServiceMonitor","metadata":{"name":"example","labels":{"k8s-app":"prometheus"}},"spec":{"selector":{"matchLabels":{"k8s-app":"prometheus","prometheus":"k8s"}},"namespaceSelector":{"matchNames":["monitoring"]},"endpoints":[{"port":"web","interval":"30s"}]}},{"apiVersion":"monitoring.coreos.com/v1","kind":"Alertmanager","metadata":{"name":"alertmanager-main"},"spec":{"replicas":3}}]'
spec:
replaces: prometheusoperator.0.14.0
displayName: Prometheus
description: |
An open-source monitoring system with a dimensional data model, flexible query language, efficient time series database and modern alerting approach.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ metadata:
annotations:
alm-examples: '[{"apiVersion":"monitoring.coreos.com/v1","kind":"Prometheus","metadata":{"name":"example","labels":{"prometheus":"k8s"}},"spec":{"replicas":2,"version":"v2.3.2","serviceAccountName":"prometheus-k8s","securityContext": {}, "serviceMonitorSelector":{"matchExpressions":[{"key":"k8s-app","operator":"Exists"}]},"ruleSelector":{"matchLabels":{"role":"prometheus-rulefiles","prometheus":"k8s"}},"alerting":{"alertmanagers":[{"namespace":"monitoring","name":"alertmanager-main","port":"web"}]}}},{"apiVersion":"monitoring.coreos.com/v1","kind":"ServiceMonitor","metadata":{"name":"example","labels":{"k8s-app":"prometheus"}},"spec":{"selector":{"matchLabels":{"k8s-app":"prometheus"}},"endpoints":[{"port":"web","interval":"30s"}]}},{"apiVersion":"monitoring.coreos.com/v1","kind":"Alertmanager","metadata":{"name":"alertmanager-main"},"spec":{"replicas":3, "securityContext": {}}}]'
spec:
replaces: prometheusoperator.0.15.0
replaces: prometheusoperator.0.14.0
displayName: Prometheus Operator
description: |
The Prometheus Operator for Kubernetes provides easy monitoring definitions for Kubernetes services and deployment and management of Prometheus instances.
Expand Down
3 changes: 3 additions & 0 deletions manifests/prometheus/prometheus.package.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
packageName: prometheus
channels:
- name: stable
currentCSV: prometheusoperator.0.15.0
- name: preview
currentCSV: prometheusoperator.0.22.2
defaultChannel: preview
15 changes: 6 additions & 9 deletions pkg/lib/bundle/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"io"
"io/ioutil"
"k8s.io/kubectl/pkg/util/slice"
"os"
"path/filepath"
"strings"
Expand All @@ -13,6 +12,7 @@ import (
"gopkg.in/yaml.v2"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
k8syaml "k8s.io/apimachinery/pkg/util/yaml"
"k8s.io/kubectl/pkg/util/slice"
)

const (
Expand Down Expand Up @@ -302,6 +302,10 @@ func ValidateChannelDefault(channels, channelDefault string) (string, error) {
var chanErr error
channelList := strings.Split(channels, ",")

if len(channelList) == 0 || slice.ContainsString(channelList, "", nil) {
return chanDefault, fmt.Errorf("invalid channels is provied: %s", channels)
}

if channelDefault != "" {
for _, channel := range channelList {
if channel == channelDefault {
Expand All @@ -313,15 +317,8 @@ func ValidateChannelDefault(channels, channelDefault string) (string, error) {
chanDefault = channelList[0]
chanErr = fmt.Errorf(`The channel list "%s" doesn't contain channelDefault "%s"`, channels, channelDefault)
}
} else {
chanDefault = channelList[0]
}

if chanDefault != "" {
return chanDefault, chanErr
} else {
return chanDefault, fmt.Errorf("Invalid channels is provied: %s", channels)
}
return chanDefault, chanErr
}

// GenerateAnnotations builds annotations.yaml with mediatype, manifests &
Expand Down
32 changes: 22 additions & 10 deletions pkg/lib/bundle/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func TestValidateChannelDefault(t *testing.T) {
{
"test5,test6",
"",
"test5",
"",
"",
},
{
Expand All @@ -72,7 +72,7 @@ func TestValidateChannelDefault(t *testing.T) {
",",
"",
"",
`Invalid channels is provied: ,`,
`invalid channels is provied: ,`,
},
}

Expand Down Expand Up @@ -207,7 +207,7 @@ func TestGenerateDockerfileFunc(t *testing.T) {
"LABEL operators.operatorframework.io.bundle.metadata.v1=%s\n"+
"LABEL operators.operatorframework.io.bundle.package.v1=test4\n"+
"LABEL operators.operatorframework.io.bundle.channels.v1=test5\n"+
"LABEL operators.operatorframework.io.bundle.channel.default.v1=test5\n\n"+
"LABEL operators.operatorframework.io.bundle.channel.default.v1=\n\n"+
"COPY test2 /manifests/\n"+
"COPY metadata /metadata/\n", MetadataDir)

Expand Down Expand Up @@ -283,11 +283,23 @@ func TestCopyYamlOutput_NestedCopy(t *testing.T) {
require.NoError(t, err)
}

func TestGenerateFunc(t *testing.T){
etcdPkgPath:="./testdata/etcd"
outputPath :="./testdata/tmp_output"
func TestGenerateFunc(t *testing.T) {
etcdPkgPath := "./testdata/etcd"
outputPath := "./testdata/tmp_output"
defer os.RemoveAll(outputPath)
err:=GenerateFunc(filepath.Join(etcdPkgPath,"0.6.1"),outputPath,"","","",true)
require.NoError(t,err)
os.Remove(filepath.Join("./",DockerFile))
}
err := GenerateFunc(filepath.Join(etcdPkgPath, "0.6.1"), outputPath, "", "", "", true)
require.NoError(t, err)
os.Remove(filepath.Join("./", DockerFile))

output := fmt.Sprintf("annotations:\n" +
" operators.operatorframework.io.bundle.channel.default.v1: \"\"\n" +
" operators.operatorframework.io.bundle.channels.v1: beta\n" +
" operators.operatorframework.io.bundle.manifests.v1: manifests/\n" +
" operators.operatorframework.io.bundle.mediatype.v1: registry+v1\n" +
" operators.operatorframework.io.bundle.metadata.v1: metadata/\n" +
" operators.operatorframework.io.bundle.package.v1: etcd\n")
outputAnnotationsFile := filepath.Join(outputPath, "metadata/", "annotations.yaml")
annotationsBlob, err := ioutil.ReadFile(outputAnnotationsFile)
require.NoError(t, err)
require.EqualValues(t, output, string(annotationsBlob))
}
6 changes: 2 additions & 4 deletions test/e2e/opm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ import (
)

var (
packageName = "prometheus"
channels = "preview"
defaultChannel = "preview"
packageName = "prometheus"

bundlePath1 = "manifests/prometheus/0.14.0"
bundlePath2 = "manifests/prometheus/0.15.0"
Expand Down Expand Up @@ -75,7 +73,7 @@ func buildBundlesWith(containerTool string) error {
bundleTag3: bundlePath3,
} {
if err := inTemporaryBuildContext(func() error {
return bundle.BuildFunc(path, "", bundleImage+":"+tag, containerTool, packageName, channels, defaultChannel, false)
return bundle.BuildFunc(path, "", bundleImage+":"+tag, containerTool, "", "", "", false)
}); err != nil {
return err
}
Expand Down

0 comments on commit 95f23b2

Please sign in to comment.