Skip to content

Commit 7ed26b7

Browse files
Allow objects to request resolution, not just images
A lot of times, you want to be able to reuse a bit of config that works fine elsewhere. By adding the `alpha.image.policy.openshift.io/resolve-names` annotation with value `*` to the object you can request that resolution attempt to match all image references to an image stream tag in the current namespace, regardless of their own policy. This makes it easy to transition to looking up image stream tags by name. Extend `oc set image-lookup` to accept `deploy/foo` and to be able to list and display non image stream resources.
1 parent 9b2743f commit 7ed26b7

File tree

14 files changed

+394
-37
lines changed

14 files changed

+394
-37
lines changed

contrib/completions/bash/oc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15869,6 +15869,8 @@ _oc_set_image-lookup()
1586915869
flags_with_completion+=("-f")
1587015870
flags_completion+=("__handle_filename_extension_flag yaml|yml|json")
1587115871
local_nonpersistent_flags+=("--filename=")
15872+
flags+=("--list")
15873+
local_nonpersistent_flags+=("--list")
1587215874
flags+=("--local")
1587315875
local_nonpersistent_flags+=("--local")
1587415876
flags+=("--no-headers")

contrib/completions/bash/openshift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21399,6 +21399,8 @@ _openshift_cli_set_image-lookup()
2139921399
flags_with_completion+=("-f")
2140021400
flags_completion+=("__handle_filename_extension_flag yaml|yml|json")
2140121401
local_nonpersistent_flags+=("--filename=")
21402+
flags+=("--list")
21403+
local_nonpersistent_flags+=("--list")
2140221404
flags+=("--local")
2140321405
local_nonpersistent_flags+=("--local")
2140421406
flags+=("--no-headers")

contrib/completions/zsh/oc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16018,6 +16018,8 @@ _oc_set_image-lookup()
1601816018
flags_with_completion+=("-f")
1601916019
flags_completion+=("__handle_filename_extension_flag yaml|yml|json")
1602016020
local_nonpersistent_flags+=("--filename=")
16021+
flags+=("--list")
16022+
local_nonpersistent_flags+=("--list")
1602116023
flags+=("--local")
1602216024
local_nonpersistent_flags+=("--local")
1602316025
flags+=("--no-headers")

contrib/completions/zsh/openshift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21548,6 +21548,8 @@ _openshift_cli_set_image-lookup()
2154821548
flags_with_completion+=("-f")
2154921549
flags_completion+=("__handle_filename_extension_flag yaml|yml|json")
2155021550
local_nonpersistent_flags+=("--filename=")
21551+
flags+=("--list")
21552+
local_nonpersistent_flags+=("--list")
2155121553
flags+=("--local")
2155221554
local_nonpersistent_flags+=("--local")
2155321555
flags+=("--no-headers")

pkg/api/meta/meta.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package meta
33
import (
44
"fmt"
55

6+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
67
"k8s.io/apimachinery/pkg/runtime"
78
"k8s.io/apimachinery/pkg/util/validation/field"
89
kapi "k8s.io/kubernetes/pkg/api"
@@ -42,3 +43,56 @@ func GetImageReferenceMutator(obj runtime.Object) (ImageReferenceMutator, error)
4243
return nil, errNoImageMutator
4344
}
4445
}
46+
47+
type AnnotationAccessor interface {
48+
// Annotations returns a map representing annotations. Not mutable.
49+
Annotations() map[string]string
50+
// SetAnnotations sets representing annotations onto the object.
51+
SetAnnotations(map[string]string)
52+
// TemplateAnnotations returns a map representing annotations on a nested template in the object. Not mutable.
53+
// If no template is present bool will be false.
54+
TemplateAnnotations() (map[string]string, bool)
55+
// SetTemplateAnnotations sets annotations on a nested template in the object.
56+
// If no template is present bool will be false.
57+
SetTemplateAnnotations(map[string]string) bool
58+
}
59+
60+
type annotationsAccessor struct {
61+
object metav1.Object
62+
template metav1.Object
63+
}
64+
65+
func (a annotationsAccessor) Annotations() map[string]string {
66+
return a.object.GetAnnotations()
67+
}
68+
69+
func (a annotationsAccessor) TemplateAnnotations() (map[string]string, bool) {
70+
if a.template == nil {
71+
return nil, false
72+
}
73+
return a.template.GetAnnotations(), true
74+
}
75+
76+
func (a annotationsAccessor) SetAnnotations(annotations map[string]string) {
77+
a.object.SetAnnotations(annotations)
78+
}
79+
80+
func (a annotationsAccessor) SetTemplateAnnotations(annotations map[string]string) bool {
81+
if a.template == nil {
82+
return false
83+
}
84+
a.template.SetAnnotations(annotations)
85+
return true
86+
}
87+
88+
// GetAnnotationAccessor returns an accessor for the provided object or false if the object
89+
// does not support accessing annotations.
90+
func GetAnnotationAccessor(obj runtime.Object) (AnnotationAccessor, bool) {
91+
switch t := obj.(type) {
92+
case metav1.Object:
93+
templateObject, _ := GetTemplateMetaObject(obj)
94+
return annotationsAccessor{object: t, template: templateObject}, true
95+
default:
96+
return nil, false
97+
}
98+
}

pkg/api/meta/pods.go

Lines changed: 83 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package meta
33
import (
44
"fmt"
55

6+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
67
"k8s.io/apimachinery/pkg/runtime"
78
"k8s.io/apimachinery/pkg/runtime/schema"
89
"k8s.io/apimachinery/pkg/util/validation/field"
@@ -92,7 +93,9 @@ func GetPodSpec(obj runtime.Object) (*kapi.PodSpec, *field.Path, error) {
9293
case *kapi.PodTemplate:
9394
return &r.Template.Spec, field.NewPath("template", "spec"), nil
9495
case *kapi.ReplicationController:
95-
return &r.Spec.Template.Spec, field.NewPath("spec", "template", "spec"), nil
96+
if r.Spec.Template != nil {
97+
return &r.Spec.Template.Spec, field.NewPath("spec", "template", "spec"), nil
98+
}
9699
case *extensions.DaemonSet:
97100
return &r.Spec.Template.Spec, field.NewPath("spec", "template", "spec"), nil
98101
case *extensions.Deployment:
@@ -114,7 +117,9 @@ func GetPodSpec(obj runtime.Object) (*kapi.PodSpec, *field.Path, error) {
114117
case *securityapi.PodSecurityPolicyReview:
115118
return &r.Spec.Template.Spec, field.NewPath("spec", "template", "spec"), nil
116119
case *deployapi.DeploymentConfig:
117-
return &r.Spec.Template.Spec, field.NewPath("spec", "template", "spec"), nil
120+
if r.Spec.Template != nil {
121+
return &r.Spec.Template.Spec, field.NewPath("spec", "template", "spec"), nil
122+
}
118123
}
119124
return nil, nil, errNoPodSpec
120125
}
@@ -129,7 +134,9 @@ func GetPodSpecV1(obj runtime.Object) (*kapiv1.PodSpec, *field.Path, error) {
129134
case *kapiv1.PodTemplate:
130135
return &r.Template.Spec, field.NewPath("template", "spec"), nil
131136
case *kapiv1.ReplicationController:
132-
return &r.Spec.Template.Spec, field.NewPath("spec", "template", "spec"), nil
137+
if r.Spec.Template != nil {
138+
return &r.Spec.Template.Spec, field.NewPath("spec", "template", "spec"), nil
139+
}
133140
case *extensionsv1beta1.DaemonSet:
134141
return &r.Spec.Template.Spec, field.NewPath("spec", "template", "spec"), nil
135142
case *extensionsv1beta1.Deployment:
@@ -153,11 +160,83 @@ func GetPodSpecV1(obj runtime.Object) (*kapiv1.PodSpec, *field.Path, error) {
153160
case *securityapiv1.PodSecurityPolicyReview:
154161
return &r.Spec.Template.Spec, field.NewPath("spec", "template", "spec"), nil
155162
case *deployapiv1.DeploymentConfig:
156-
return &r.Spec.Template.Spec, field.NewPath("spec", "template", "spec"), nil
163+
if r.Spec.Template != nil {
164+
return &r.Spec.Template.Spec, field.NewPath("spec", "template", "spec"), nil
165+
}
157166
}
158167
return nil, nil, errNoPodSpec
159168
}
160169

170+
// GetTemplateMetaObject returns a mutable metav1.Object interface for the template
171+
// the object contains, or false if no such object is available.
172+
func GetTemplateMetaObject(obj runtime.Object) (metav1.Object, bool) {
173+
switch r := obj.(type) {
174+
case *kapiv1.PodTemplate:
175+
return &r.Template.ObjectMeta, true
176+
case *kapiv1.ReplicationController:
177+
if r.Spec.Template != nil {
178+
return &r.Spec.Template.ObjectMeta, true
179+
}
180+
case *extensionsv1beta1.DaemonSet:
181+
return &r.Spec.Template.ObjectMeta, true
182+
case *extensionsv1beta1.Deployment:
183+
return &r.Spec.Template.ObjectMeta, true
184+
case *extensionsv1beta1.ReplicaSet:
185+
return &r.Spec.Template.ObjectMeta, true
186+
case *batchv1.Job:
187+
return &r.Spec.Template.ObjectMeta, true
188+
case *batchv2alpha1.CronJob:
189+
return &r.Spec.JobTemplate.Spec.Template.ObjectMeta, true
190+
case *batchv2alpha1.JobTemplate:
191+
return &r.Template.Spec.Template.ObjectMeta, true
192+
case *appsv1beta1.StatefulSet:
193+
return &r.Spec.Template.ObjectMeta, true
194+
case *appsv1beta1.Deployment:
195+
return &r.Spec.Template.ObjectMeta, true
196+
case *securityapiv1.PodSecurityPolicySubjectReview:
197+
return &r.Spec.Template.ObjectMeta, true
198+
case *securityapiv1.PodSecurityPolicySelfSubjectReview:
199+
return &r.Spec.Template.ObjectMeta, true
200+
case *securityapiv1.PodSecurityPolicyReview:
201+
return &r.Spec.Template.ObjectMeta, true
202+
case *deployapiv1.DeploymentConfig:
203+
if r.Spec.Template != nil {
204+
return &r.Spec.Template.ObjectMeta, true
205+
}
206+
case *kapi.PodTemplate:
207+
return &r.Template.ObjectMeta, true
208+
case *kapi.ReplicationController:
209+
if r.Spec.Template != nil {
210+
return &r.Spec.Template.ObjectMeta, true
211+
}
212+
case *extensions.DaemonSet:
213+
return &r.Spec.Template.ObjectMeta, true
214+
case *extensions.Deployment:
215+
return &r.Spec.Template.ObjectMeta, true
216+
case *extensions.ReplicaSet:
217+
return &r.Spec.Template.ObjectMeta, true
218+
case *batch.Job:
219+
return &r.Spec.Template.ObjectMeta, true
220+
case *batch.CronJob:
221+
return &r.Spec.JobTemplate.Spec.Template.ObjectMeta, true
222+
case *batch.JobTemplate:
223+
return &r.Template.Spec.Template.ObjectMeta, true
224+
case *apps.StatefulSet:
225+
return &r.Spec.Template.ObjectMeta, true
226+
case *securityapi.PodSecurityPolicySubjectReview:
227+
return &r.Spec.Template.ObjectMeta, true
228+
case *securityapi.PodSecurityPolicySelfSubjectReview:
229+
return &r.Spec.Template.ObjectMeta, true
230+
case *securityapi.PodSecurityPolicyReview:
231+
return &r.Spec.Template.ObjectMeta, true
232+
case *deployapi.DeploymentConfig:
233+
if r.Spec.Template != nil {
234+
return &r.Spec.Template.ObjectMeta, true
235+
}
236+
}
237+
return nil, false
238+
}
239+
161240
type containerMutator struct {
162241
*kapi.Container
163242
}

0 commit comments

Comments
 (0)