Skip to content

Commit 251813d

Browse files
committed
feat: update crd, format crd, fix annotation and debug
Signed-off-by: will <[email protected]>
1 parent bd0f650 commit 251813d

File tree

4 files changed

+91
-5
lines changed

4 files changed

+91
-5
lines changed

config/crd/bases/imageshift.dev_imageshifts.yaml

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,79 @@ spec:
8989
type: object
9090
status:
9191
description: ImageshiftStatus defines the observed state of Imageshift.
92+
properties:
93+
conditions:
94+
description: Conditions represent the latest available observations
95+
of the Imageshift's state
96+
items:
97+
description: Condition contains details for one aspect of the current
98+
state of this API Resource.
99+
properties:
100+
lastTransitionTime:
101+
description: |-
102+
lastTransitionTime is the last time the condition transitioned from one status to another.
103+
This should be when the underlying condition changed. If that is not known, then using the time when the API field changed is acceptable.
104+
format: date-time
105+
type: string
106+
message:
107+
description: |-
108+
message is a human readable message indicating details about the transition.
109+
This may be an empty string.
110+
maxLength: 32768
111+
type: string
112+
observedGeneration:
113+
description: |-
114+
observedGeneration represents the .metadata.generation that the condition was set based upon.
115+
For instance, if .metadata.generation is currently 12, but the .status.conditions[x].observedGeneration is 9, the condition is out of date
116+
with respect to the current state of the instance.
117+
format: int64
118+
minimum: 0
119+
type: integer
120+
reason:
121+
description: |-
122+
reason contains a programmatic identifier indicating the reason for the condition's last transition.
123+
Producers of specific condition types may define expected values and meanings for this field,
124+
and whether the values are considered a guaranteed API.
125+
The value should be a CamelCase string.
126+
This field may not be empty.
127+
maxLength: 1024
128+
minLength: 1
129+
pattern: ^[A-Za-z]([A-Za-z0-9_,:]*[A-Za-z0-9_])?$
130+
type: string
131+
status:
132+
description: status of the condition, one of True, False, Unknown.
133+
enum:
134+
- "True"
135+
- "False"
136+
- Unknown
137+
type: string
138+
type:
139+
description: type of condition in CamelCase or in foo.example.com/CamelCase.
140+
maxLength: 316
141+
pattern: ^([a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*/)?(([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9])$
142+
type: string
143+
required:
144+
- lastTransitionTime
145+
- message
146+
- reason
147+
- status
148+
- type
149+
type: object
150+
type: array
151+
configValid:
152+
description: ConfigValid indicates whether the current configuration
153+
is valid
154+
type: boolean
155+
lastReconciled:
156+
description: LastReconciled is the timestamp of the last successful
157+
reconciliation
158+
format: date-time
159+
type: string
160+
mutatedPodCount:
161+
description: MutatedPodCount tracks the number of pods that have been
162+
mutated
163+
format: int64
164+
type: integer
92165
type: object
93166
type: object
94167
served: true

config/rbac/role.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ rules:
1717
resources:
1818
- pods
1919
verbs:
20+
- delete
2021
- get
2122
- list
2223
- watch
23-
- delete
2424
- apiGroups:
2525
- imageshift.dev
2626
resources:

internal/webhook/v1/pod_webhook.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,12 @@ func (d *PodCustomDefaulter) Default(ctx context.Context, pod *corev1.Pod) error
133133
hasChanged = true
134134

135135
annotation := fmt.Sprintf("%s.container.imageshift.dev/original", pod.Spec.Containers[i].Name)
136-
pod.Annotations[annotation] = pod.Spec.Containers[i].Image
136+
originalImage := pod.Spec.Containers[i].Image
137+
pod.Annotations[annotation] = originalImage
137138

138139
pod.Spec.Containers[i].Image = img
139140

140-
podlog.Info("Patched Container", "pod", container.Name, "reference", img)
141+
podlog.Info("Patched Container", "container", container.Name, "reference", img, "originalImage", originalImage, "annotationKey", annotation)
141142
}
142143
}
143144
} else {

pkg/swap/swap.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,20 +42,32 @@ func getCompiledRegex(expression string) (*regexp.Regexp, error) {
4242
return re, nil
4343
}
4444

45+
// normalizeRegistry returns the canonical registry name using go-containerregistry
46+
func normalizeRegistry(registry string) string {
47+
// Parse a dummy image with the registry to get the canonical form
48+
ref, err := name.ParseReference(registry + "/dummy:latest")
49+
if err != nil {
50+
return registry
51+
}
52+
return ref.Context().RegistryStr()
53+
}
54+
4555
func SwapImage(config imageshiftv1.Imageshift, image string) string {
4656
ref, _ := name.ParseReference(image, name.WithDefaultRegistry(config.Spec.Default))
4757

4858
registry := ref.Context().RegistryStr()
59+
normalizedDefault := normalizeRegistry(config.Spec.Default)
4960

5061
// if registry == default registry return image
5162
var newImage string
5263

53-
if registry == config.Spec.Default {
64+
if registry == normalizedDefault {
5465
newImage = ref.Name()
5566
}
5667

5768
for _, swap := range config.Spec.Mappings.Swap {
58-
if swap.Registry == registry {
69+
normalizedSwapRegistry := normalizeRegistry(swap.Registry)
70+
if normalizedSwapRegistry == registry {
5971
identifier := ref.Identifier()
6072
switch len(strings.Split(identifier, ":")) {
6173
case 1:

0 commit comments

Comments
 (0)