-
Notifications
You must be signed in to change notification settings - Fork 249
/
output_image_tag.go
174 lines (153 loc) · 5.42 KB
/
output_image_tag.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
package steps
import (
"context"
"fmt"
"time"
"github.com/sirupsen/logrus"
coreapi "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/wait"
crclient "sigs.k8s.io/controller-runtime/pkg/client"
crcontrollerutil "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
imagev1 "github.com/openshift/api/image/v1"
"github.com/openshift/ci-tools/pkg/api"
"github.com/openshift/ci-tools/pkg/results"
"github.com/openshift/ci-tools/pkg/steps/loggingclient"
"github.com/openshift/ci-tools/pkg/steps/utils"
)
// outputImageTagStep will ensure that a tag exists
// in the named ImageStream that resolves to the built
// pipeline image
type outputImageTagStep struct {
config api.OutputImageTagStepConfiguration
client loggingclient.LoggingClient
jobSpec *api.JobSpec
}
func (s *outputImageTagStep) Inputs() (api.InputDefinition, error) {
return nil, nil
}
func (*outputImageTagStep) Validate() error { return nil }
func (s *outputImageTagStep) Run(ctx context.Context) error {
return results.ForReason("tagging_output_image").ForError(s.run(ctx))
}
func (s *outputImageTagStep) run(ctx context.Context) error {
toNamespace := s.namespace()
if string(s.config.From) == s.config.To.Tag && toNamespace == s.jobSpec.Namespace() && s.config.To.Name == api.StableImageStream {
logrus.Infof("Tagging %s into %s", s.config.From, s.config.To.Name)
} else {
logrus.Infof("Tagging %s into %s", s.config.From, s.config.To.ISTagName())
}
from := &imagev1.ImageStreamTag{}
namespace := s.jobSpec.Namespace()
name := fmt.Sprintf("%s:%s", api.PipelineImageStream, s.config.From)
if err := s.client.Get(ctx, crclient.ObjectKey{
Namespace: namespace,
Name: name,
}, from); err != nil {
return fmt.Errorf("could not resolve base image from %s/%s: %w", namespace, name, err)
}
desired := s.imageStreamTag(from.Image.Name)
ist := &imagev1.ImageStreamTag{
ObjectMeta: metav1.ObjectMeta{
Namespace: desired.ObjectMeta.Namespace,
Name: desired.ObjectMeta.Name,
},
}
// Retry on conflicts with exponential backoff to avoid thundering. Note that `Patch` is
// not supposed return a conflict so in theory we should not need it but we do:
// > Clayton Coleman 6 hours ago
// > i think we may have found a bug in kube, which is exciting
if waitErr := wait.ExponentialBackoff(wait.Backoff{Steps: 4, Factor: 2, Duration: time.Second}, func() (bool, error) {
_, err := crcontrollerutil.CreateOrPatch(ctx, s.client, ist, func() error {
ist.Tag = desired.Tag
return nil
})
switch {
case err != nil && errors.IsConflict(err):
return false, nil
case err != nil && errors.IsAlreadyExists(err):
return true, nil
case err != nil:
return false, err
}
return true, nil
}); waitErr != nil {
return fmt.Errorf("could not upsert output imagestreamtag: %w", waitErr)
}
return nil
}
func (s *outputImageTagStep) Requires() []api.StepLink {
return []api.StepLink{
api.InternalImageLink(s.config.From),
// Release input and import steps do not handle the
// case when other steps are publishing tags to the
// stable stream. Generally, this is not an issue as
// the former run at the start of execution and the
// latter only once images are built. However, in
// specific configurations, authors may create an
// execution graph where we race.
api.ReleaseImagesLink(api.LatestReleaseName),
}
}
func (s *outputImageTagStep) Creates() (ret []api.StepLink) {
ret = append(ret, api.ExternalImageLink(s.config.To))
if len(s.config.To.As) > 0 {
ret = append(ret, api.InternalImageLink(api.PipelineImageStreamTagReference(s.config.To.As)))
}
return
}
func (s *outputImageTagStep) Provides() api.ParameterMap {
if len(s.config.To.As) == 0 {
return nil
}
return api.ParameterMap{
utils.StableImageEnv(s.config.To.As): utils.ImageDigestFor(s.client, func() string {
return s.config.To.Namespace
}, s.config.To.Name, s.config.To.Tag),
}
}
func (s *outputImageTagStep) Name() string { return s.config.TargetName() }
func (s *outputImageTagStep) Description() string {
if len(s.config.To.As) == 0 {
return fmt.Sprintf("Tag the image %s into the image stream tag %s:%s", s.config.From, s.config.To.Name, s.config.To.Tag)
}
return fmt.Sprintf("Tag the image %s into the stable image stream", s.config.From)
}
func (s *outputImageTagStep) Objects() []crclient.Object {
return s.client.Objects()
}
func (s *outputImageTagStep) namespace() string {
if len(s.config.To.Namespace) != 0 {
return s.config.To.Namespace
}
return s.jobSpec.Namespace()
}
func (s *outputImageTagStep) imageStreamTag(fromImage string) *imagev1.ImageStreamTag {
return &imagev1.ImageStreamTag{
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("%s:%s", s.config.To.Name, s.config.To.Tag),
Namespace: s.namespace(),
},
Tag: &imagev1.TagReference{
ReferencePolicy: imagev1.TagReferencePolicy{
Type: imagev1.LocalTagReferencePolicy,
},
ImportPolicy: imagev1.TagImportPolicy{
ImportMode: imagev1.ImportModePreserveOriginal,
},
From: &coreapi.ObjectReference{
Kind: "ImageStreamImage",
Name: fmt.Sprintf("%s@%s", api.PipelineImageStream, fromImage),
Namespace: s.jobSpec.Namespace(),
},
},
}
}
func OutputImageTagStep(config api.OutputImageTagStepConfiguration, client loggingclient.LoggingClient, jobSpec *api.JobSpec) api.Step {
return &outputImageTagStep{
config: config,
client: client,
jobSpec: jobSpec,
}
}