-
Notifications
You must be signed in to change notification settings - Fork 249
/
index_generator.go
217 lines (197 loc) · 7.82 KB
/
index_generator.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package steps
import (
"context"
"fmt"
"strings"
"github.com/sirupsen/logrus"
coreapi "k8s.io/api/core/v1"
ctrlruntimeclient "sigs.k8s.io/controller-runtime/pkg/client"
buildapi "github.com/openshift/api/build/v1"
imagev1 "github.com/openshift/api/image/v1"
"github.com/openshift/ci-tools/pkg/api"
"github.com/openshift/ci-tools/pkg/api/helper"
"github.com/openshift/ci-tools/pkg/kubernetes"
"github.com/openshift/ci-tools/pkg/results"
"github.com/openshift/ci-tools/pkg/steps/utils"
)
type indexGeneratorStep struct {
config api.IndexGeneratorStepConfiguration
releaseBuildConfig *api.ReleaseBuildConfiguration
resources api.ResourceConfiguration
client BuildClient
podClient kubernetes.PodClient
jobSpec *api.JobSpec
pullSecret *coreapi.Secret
}
const IndexDataDirectory = "/index-data"
const IndexDockerfileName = "index.Dockerfile"
func (s *indexGeneratorStep) Inputs() (api.InputDefinition, error) {
return nil, nil
}
func (*indexGeneratorStep) Validate() error { return nil }
func databaseIndex(client ctrlruntimeclient.Client, name, namespace string) (bool, error) {
ist := &imagev1.ImageStreamTag{}
ctx := context.TODO()
if err := client.Get(ctx, ctrlruntimeclient.ObjectKey{Namespace: namespace, Name: name}, ist); err != nil {
return false, fmt.Errorf("could not fetch source ImageStreamTag: %w", err)
}
// At the moment, we support only amd64
labels, err := helper.LabelsOnISTagImage(ctx, client, ist, api.ReleaseArchitectureAMD64)
if err != nil {
return false, fmt.Errorf("failed to get value of the image label: %w", err)
}
if labels == nil {
return false, nil
}
_, ok := labels["operators.operatorframework.io.index.database.v1"]
return ok, nil
}
func (s *indexGeneratorStep) Run(ctx context.Context) error {
return results.ForReason("building_index_generator").ForError(s.run(ctx))
}
func (s *indexGeneratorStep) run(ctx context.Context) error {
logrus.Warn("DEPRECATION WARNING: Building index images is deprecated and will be removed from ci-operator soon. See https://docs.ci.openshift.org/docs/how-tos/testing-operator-sdk-operators/#moving-to-file-based-catalog for details.")
source := fmt.Sprintf("%s:%s", api.PipelineImageStream, api.PipelineImageStreamTagReferenceSource)
workingDir, err := getWorkingDir(s.client, source, s.jobSpec.Namespace())
if err != nil {
return fmt.Errorf("failed to get workingDir: %w", err)
}
if s.config.BaseIndex != "" {
source := fmt.Sprintf("%s:%s", api.PipelineImageStream, s.config.BaseIndex)
ok, err := databaseIndex(s.client, source, s.jobSpec.Namespace())
if err != nil {
return fmt.Errorf("failed to determine if the image %s/%s is sqlite based index: %w", s.jobSpec.Namespace(), source, err)
}
if !ok {
logrus.Warn("Skipped building the index image: opm index commands, which are used by the ci-operator, interact only with a database index, but the base index is not one. Please refer to the FBC docs here: https://olm.operatorframework.io/docs/reference/file-based-catalogs/.")
return nil
} else {
logrus.Debug("The base index image is sqlite based")
}
}
dockerfile, err := s.indexGenDockerfile()
if err != nil {
return err
}
fromTag := api.PipelineImageStreamTagReferenceSource
fromDigest, err := resolvePipelineImageStreamTagReference(ctx, s.client, fromTag, s.jobSpec)
if err != nil {
return err
}
var secrets []buildapi.SecretBuildSource
if s.pullSecret != nil {
secrets = append(secrets, buildapi.SecretBuildSource{
Secret: coreapi.LocalObjectReference{Name: s.pullSecret.Name},
})
}
build := buildFromSource(
s.jobSpec, fromTag, s.config.To,
buildapi.BuildSource{
Type: buildapi.BuildSourceDockerfile,
Dockerfile: &dockerfile,
Images: []buildapi.ImageSource{
{
From: coreapi.ObjectReference{
Kind: "ImageStreamTag",
Name: source,
},
Paths: []buildapi.ImageSourcePath{{
SourcePath: fmt.Sprintf("%s/.", workingDir),
DestinationDir: ".",
}},
},
},
Secrets: secrets,
},
fromDigest,
"",
s.resources,
s.pullSecret,
nil,
"",
)
err = handleBuilds(ctx, s.client, s.podClient, *build)
if err != nil && strings.Contains(err.Error(), "error checking provided apis") {
return results.ForReason("generating_index").WithError(err).Errorf("failed to generate operator index due to invalid bundle info: %v", err)
}
return err
}
func (s *indexGeneratorStep) indexGenDockerfile() (string, error) {
var dockerCommands []string
dockerCommands = append(dockerCommands, "FROM quay.io/operator-framework/upstream-opm-builder AS builder")
if s.pullSecret != nil {
dockerCommands = append(dockerCommands, "COPY .dockerconfigjson .")
dockerCommands = append(dockerCommands, "RUN mkdir $HOME/.docker && mv .dockerconfigjson $HOME/.docker/config.json")
}
var bundles []string
for _, bundleName := range s.config.OperatorIndex {
fullSpec, err := utils.ImageDigestFor(s.client, s.jobSpec.Namespace, api.PipelineImageStream, bundleName)()
if err != nil {
return "", fmt.Errorf("failed to get image digest for bundle `%s`: %w", bundleName, err)
}
bundles = append(bundles, fullSpec)
}
baseIndex := ""
if s.config.BaseIndex != "" {
fullSpec, err := utils.ImageDigestFor(s.client, s.jobSpec.Namespace, api.PipelineImageStream, s.config.BaseIndex)()
if err != nil {
return "", fmt.Errorf("failed to get image digest for bundle `%s`: %w", s.config.BaseIndex, err)
}
baseIndex = fullSpec
}
opmCommand := fmt.Sprintf(`RUN ["opm", "index", "add", "--mode", "%s", "--bundles", "%s", "--out-dockerfile", "%s", "--generate"`, s.config.UpdateGraph, strings.Join(bundles, ","), IndexDockerfileName)
if baseIndex != "" {
opmCommand = fmt.Sprintf(`%s, "--from-index", "%s"`, opmCommand, baseIndex)
}
opmCommand = fmt.Sprintf("%s]", opmCommand)
dockerCommands = append(dockerCommands, opmCommand)
dockerCommands = append(dockerCommands, fmt.Sprintf("FROM %s:%s", api.PipelineImageStream, api.PipelineImageStreamTagReferenceSource))
dockerCommands = append(dockerCommands, fmt.Sprintf("WORKDIR %s", IndexDataDirectory))
dockerCommands = append(dockerCommands, fmt.Sprintf("COPY --from=builder %s %s", IndexDockerfileName, IndexDockerfileName))
dockerCommands = append(dockerCommands, "COPY --from=builder /database/ database")
return strings.Join(dockerCommands, "\n"), nil
}
func (s *indexGeneratorStep) Requires() []api.StepLink {
var links []api.StepLink
for _, bundle := range s.config.OperatorIndex {
imageStream, name, _ := s.releaseBuildConfig.DependencyParts(api.StepDependency{Name: bundle}, nil)
links = append(links, api.LinkForImage(imageStream, name))
}
if s.config.BaseIndex != "" {
imageStream, name, _ := s.releaseBuildConfig.DependencyParts(api.StepDependency{Name: s.config.BaseIndex}, nil)
links = append(links, api.LinkForImage(imageStream, name))
}
return links
}
func (s *indexGeneratorStep) Creates() []api.StepLink {
return []api.StepLink{api.InternalImageLink(s.config.To)}
}
func (s *indexGeneratorStep) Provides() api.ParameterMap {
return api.ParameterMap{}
}
func (s *indexGeneratorStep) Name() string { return s.config.TargetName() }
func (s *indexGeneratorStep) Description() string {
return fmt.Sprintf("Build image %s from the repository", s.config.To)
}
func (s *indexGeneratorStep) Objects() []ctrlruntimeclient.Object {
return s.client.Objects()
}
func IndexGeneratorStep(
config api.IndexGeneratorStepConfiguration,
releaseBuildConfig *api.ReleaseBuildConfiguration,
resources api.ResourceConfiguration,
buildClient BuildClient,
podClient kubernetes.PodClient,
jobSpec *api.JobSpec,
pullSecret *coreapi.Secret,
) api.Step {
return &indexGeneratorStep{
config: config,
releaseBuildConfig: releaseBuildConfig,
resources: resources,
client: buildClient,
podClient: podClient,
jobSpec: jobSpec,
pullSecret: pullSecret,
}
}