Skip to content

Commit 3a371ae

Browse files
committed
feat: mirror images according to ClusterImageSetMirrors
1 parent a7f2698 commit 3a371ae

File tree

8 files changed

+661
-45
lines changed

8 files changed

+661
-45
lines changed

api/kuik/v1alpha1/clusterimagesetmirror_types.go

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,15 @@ import (
44
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
55
)
66

7-
// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
8-
// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized.
9-
107
// ClusterImageSetMirrorSpec defines the desired state of ClusterImageSetMirror.
11-
type ClusterImageSetMirrorSpec struct {
12-
// INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
13-
// Important: Run "make" to regenerate code after modifying this file
14-
15-
// Foo is an example field of ClusterImageSetMirror. Edit clusterimagesetmirror_types.go to remove/update
16-
Foo string `json:"foo,omitempty"`
17-
}
8+
type ClusterImageSetMirrorSpec ImageSetMirrorSpec
189

1910
// ClusterImageSetMirrorStatus defines the observed state of ClusterImageSetMirror.
20-
type ClusterImageSetMirrorStatus struct {
21-
// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
22-
// Important: Run "make" to regenerate code after modifying this file
23-
}
11+
type ClusterImageSetMirrorStatus ImageSetMirrorStatus
2412

2513
// +kubebuilder:object:root=true
2614
// +kubebuilder:subresource:status
27-
// +kubebuilder:resource:scope=Cluster
15+
// +kubebuilder:resource:scope=Cluster,shortName=cism
2816

2917
// ClusterImageSetMirror is the Schema for the clusterimagesetmirrors API.
3018
type ClusterImageSetMirror struct {

api/kuik/v1alpha1/imagesetmirror_types.go

Lines changed: 58 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
11
package v1alpha1
22

33
import (
4+
"path"
5+
"strings"
6+
47
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
58
)
69

7-
// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
8-
// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized.
9-
1010
// ImageSetMirrorSpec defines the desired state of ImageSetMirror.
1111
type ImageSetMirrorSpec struct {
12-
// INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
13-
// Important: Run "make" to regenerate code after modifying this file
14-
15-
// Foo is an example field of ImageSetMirror. Edit imagesetmirror_types.go to remove/update
16-
Foo string `json:"foo,omitempty"`
12+
ImageMatcher string `json:"imageMatcher,omitempty"`
13+
Cleanup Cleanup `json:"cleanup,omitempty"`
14+
Mirrors Mirrors `json:"mirrors,omitempty"`
1715
}
1816

1917
// ImageSetMirrorStatus defines the observed state of ImageSetMirror.
2018
type ImageSetMirrorStatus struct {
21-
// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
22-
// Important: Run "make" to regenerate code after modifying this file
19+
// +listType=map
20+
// +listMapKey=image
21+
MatchedImages []MatchedImage `json:"matchedImages,omitempty"`
2322
}
2423

2524
// +kubebuilder:object:root=true
2625
// +kubebuilder:subresource:status
26+
// +kubebuilder:resource:shortName=ism
2727

2828
// ImageSetMirror is the Schema for the imagesetmirrors API.
2929
type ImageSetMirror struct {
@@ -43,6 +43,54 @@ type ImageSetMirrorList struct {
4343
Items []ImageSetMirror `json:"items"`
4444
}
4545

46+
// Cleanup defines a cleanup strategy
47+
type Cleanup struct {
48+
Enabled bool `json:"enabled,omitempty"`
49+
Retention metav1.Duration `json:"retention,omitempty"`
50+
}
51+
52+
type Mirror struct {
53+
Registry string `json:"registry,omitempty"`
54+
Path string `json:"path,omitempty"`
55+
CredentialSecret *CredentialSecret `json:"credentialSecret,omitempty"`
56+
Cleanup *Cleanup `json:"cleanup,omitempty"`
57+
}
58+
59+
type Mirrors []Mirror
60+
61+
type CredentialSecret struct {
62+
// Name is the name of the secret
63+
Name string `json:"name,omitempty"`
64+
// Namespace is the namespace where the secret is located.
65+
// This value is ignored for namespaced resources and the namespace of the parent object is used instead.
66+
Namespace string `json:"namespace,omitempty"` // TODO: make this field required for ClusterImageSetMirrors and prohibited for ImageSetMirrors
67+
}
68+
69+
type MatchedImage struct {
70+
Image string `json:"image"`
71+
// +listType=map
72+
// +listMapKey=image
73+
Mirrors []MirrorStatus `json:"mirrors,omitempty"`
74+
UnusedSince *metav1.Duration `json:"unusedSince,omitempty"`
75+
}
76+
77+
type MirrorStatus struct {
78+
Image string `json:"image"`
79+
MirroredAt *metav1.Time `json:"mirroredAt,omitempty"`
80+
}
81+
4682
func init() {
4783
SchemeBuilder.Register(&ImageSetMirror{}, &ImageSetMirrorList{})
4884
}
85+
86+
func (m Mirrors) GetCredentialSecretForImage(image string) (cred *CredentialSecret) {
87+
longestPrefixLen := 0
88+
for _, mirror := range m {
89+
prefix := path.Join(mirror.Registry, mirror.Path)
90+
if strings.HasPrefix(image, prefix) && len(prefix) > longestPrefixLen {
91+
cred = mirror.CredentialSecret
92+
longestPrefixLen = len(prefix)
93+
}
94+
}
95+
return
96+
}

api/kuik/v1alpha1/zz_generated.deepcopy.go

Lines changed: 158 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)