This repository was archived by the owner on Dec 11, 2021. It is now read-only.
forked from operator-framework/operator-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpackage_manifest.go
170 lines (149 loc) · 5.22 KB
/
package_manifest.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// Copyright 2019 The Operator-SDK 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 catalog
import (
"io/ioutil"
"os"
"path/filepath"
"sort"
"strings"
"github.com/operator-framework/operator-sdk/internal/pkg/scaffold"
"github.com/operator-framework/operator-sdk/internal/pkg/scaffold/input"
registryutil "github.com/operator-framework/operator-sdk/internal/util/operator-registry"
"github.com/ghodss/yaml"
olmregistry "github.com/operator-framework/operator-lifecycle-manager/pkg/controller/registry"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/spf13/afero"
)
const PackageManifestFileExt = ".package.yaml"
type PackageManifest struct {
input.Input
// CSVVersion is the version of the CSV being updated.
CSVVersion string
// Channel is CSVVersion's package manifest channel. If a new package
// manifest is generated, this channel will be the manifest default.
Channel string
// If ChannelIsDefault is true, Channel will be the package manifests'
// default channel.
ChannelIsDefault bool
// OperatorName is the operator's name, ex. app-operator
OperatorName string
}
var _ input.File = &PackageManifest{}
// GetInput gets s' Input.
func (s *PackageManifest) GetInput() (input.Input, error) {
if s.Path == "" {
lowerOperatorName := strings.ToLower(s.OperatorName)
// Path is what the operator-registry expects:
// {manifests -> olm-catalog}/{operator_name}/{operator_name}.package.yaml
s.Path = filepath.Join(OLMCatalogDir, lowerOperatorName,
lowerOperatorName+PackageManifestFileExt)
}
return s.Input, nil
}
var _ scaffold.CustomRenderer = &PackageManifest{}
// SetFS is a no-op to implement CustomRenderer.
func (s *PackageManifest) SetFS(_ afero.Fs) {}
// CustomRender either reads an existing package manifest or creates a new
// manifest and modifies it based on values set in s.
func (s *PackageManifest) CustomRender() ([]byte, error) {
i, err := s.GetInput()
if err != nil {
return nil, err
}
path := filepath.Join(s.AbsProjectPath, i.Path)
pm := &olmregistry.PackageManifest{}
if _, err := os.Stat(path); err != nil && os.IsNotExist(err) {
pm = s.newPackageManifest()
} else if err == nil {
b, err := ioutil.ReadFile(path)
if err != nil {
return nil, errors.Wrapf(err, "failed to read package manifest %s", path)
}
if len(b) > 0 {
if err = yaml.Unmarshal(b, pm); err != nil {
return nil, errors.Wrapf(err, "failed to unmarshal package manifest %s", path)
}
} else {
// File exists but is empty.
pm = s.newPackageManifest()
}
} else {
return nil, errors.Wrapf(err, "package manifest %s", path)
}
if err := registryutil.ValidatePackageManifest(pm); err != nil {
return nil, errors.Wrapf(err, "failed to validate package manifest %s", pm.PackageName)
}
if err = s.setChannels(pm); err != nil {
return nil, err
}
sort.Slice(pm.Channels, func(i int, j int) bool {
return pm.Channels[i].Name < pm.Channels[j].Name
})
return yaml.Marshal(pm)
}
func (s *PackageManifest) newPackageManifest() *olmregistry.PackageManifest {
// Take the current CSV version to be the "alpha" channel, as an operator
// should only be designated anything more stable than "alpha" by a human.
channel := "alpha"
if s.Channel != "" {
channel = s.Channel
}
lowerOperatorName := strings.ToLower(s.OperatorName)
pm := &olmregistry.PackageManifest{
PackageName: lowerOperatorName,
Channels: []olmregistry.PackageChannel{
{Name: channel, CurrentCSVName: getCSVName(lowerOperatorName, s.CSVVersion)},
},
DefaultChannelName: channel,
}
return pm
}
// setChannels checks for duplicate channels in pm and sets the default
// channel if possible.
func (s *PackageManifest) setChannels(pm *olmregistry.PackageManifest) error {
if s.Channel != "" {
channelIdx := sort.Search(len(pm.Channels), func(i int) bool {
return pm.Channels[i].Name == s.Channel
})
lowerOperatorName := strings.ToLower(s.OperatorName)
if channelIdx == len(pm.Channels) {
pm.Channels = append(pm.Channels, olmregistry.PackageChannel{
Name: s.Channel,
CurrentCSVName: getCSVName(lowerOperatorName, s.CSVVersion),
})
} else {
pm.Channels[channelIdx].CurrentCSVName = getCSVName(lowerOperatorName, s.CSVVersion)
}
// Use s.Channel as the default channel if caller has specified it as the
// default.
if s.ChannelIsDefault {
pm.DefaultChannelName = s.Channel
}
}
if pm.DefaultChannelName == "" {
log.Warn("Package manifest default channel is empty and should be set to an existing channel.")
}
defaultExists := false
for _, c := range pm.Channels {
if pm.DefaultChannelName == c.Name {
defaultExists = true
}
}
if !defaultExists {
log.Warnf("Package manifest default channel %s does not exist in channels.", pm.DefaultChannelName)
}
return nil
}