feat.imagehub mutate image cr. (#2377)

* mutate image cr.

* add mutate func

* add image type.
This commit is contained in:
lingdie
2023-01-11 17:51:36 +08:00
committed by GitHub
parent 9366c794fd
commit 250115c3e3
7 changed files with 56 additions and 20 deletions
+13 -4
View File
@@ -36,6 +36,7 @@ const (
type Data struct {
// base
Name ImageName `json:"name,omitempty"`
Type ImageType `json:"type,omitempty"`
// grid
RepoName RepoName `json:"repoName,omitempty"`
Rating int `json:"rating,omitempty"`
@@ -63,19 +64,23 @@ type FullData struct {
type ImageBaseData struct {
Name ImageName `json:"name,omitempty"`
Type ImageType `json:"type,omitempty"`
}
func (i *ImageBaseData) New(fd *FullData) {
i.Name = fd.ImageInfo.Name
i.Type = fd.ImageInfo.Type
}
func (i *ImageBaseData) ToData() Data {
return Data{
Name: i.Name,
Type: i.Type,
}
}
type ImageGridData struct {
Name ImageName `json:"name,omitempty"`
Type ImageType `json:"type,omitempty"`
RepoName RepoName `json:"repoName,omitempty"`
Rating int `json:"rating,omitempty"`
Icon string `json:"icon,omitempty"`
@@ -85,18 +90,19 @@ type ImageGridData struct {
}
func (i *ImageGridData) New(fd *FullData) {
i.RepoName = fd.RepoInfo.Name
i.Name = fd.ImageInfo.Name
i.Type = fd.ImageInfo.Type
i.RepoName = fd.RepoInfo.Name
i.Icon = fd.ImageInfo.DetailInfo.Icon
i.Keywords = fd.ImageInfo.DetailInfo.Keywords
i.Size = fd.ImageInfo.DetailInfo.Size
i.Description = fd.ImageInfo.DetailInfo.Description
}
func (i *ImageGridData) ToData() Data {
return Data{
RepoName: i.RepoName,
Name: i.Name,
Type: i.Type,
RepoName: i.RepoName,
Icon: i.Icon,
Keywords: i.Keywords,
Rating: i.Rating,
@@ -107,6 +113,7 @@ func (i *ImageGridData) ToData() Data {
type ImageDetailData struct {
Name ImageName `json:"name,omitempty"`
Type ImageType `json:"type,omitempty"`
RepoName RepoName `json:"repoName,omitempty"`
Rating int `json:"rating,omitempty"`
Icon string `json:"icon,omitempty"`
@@ -121,6 +128,7 @@ type ImageDetailData struct {
func (i *ImageDetailData) New(fd *FullData) {
i.Name = fd.ImageInfo.Name
i.Type = fd.ImageInfo.Type
i.RepoName = fd.RepoInfo.Name
i.Icon = fd.ImageInfo.DetailInfo.Icon
@@ -135,8 +143,9 @@ func (i *ImageDetailData) New(fd *FullData) {
}
func (i *ImageDetailData) ToData() Data {
return Data{
RepoName: i.RepoName,
Name: i.Name,
Type: i.Type,
RepoName: i.RepoName,
Icon: i.Icon,
Keywords: i.Keywords,
Size: i.Size,
@@ -32,6 +32,13 @@ type TagData struct {
CTime metav1.Time `json:"creatTime"` // todo inspect image and get time
}
type ImageType string
const (
CloudImageType ImageType = "cloud-image"
ClusterImageType ImageType = "cluster-image"
)
type ImageName string
// IsLegal check name is legal
@@ -120,6 +127,7 @@ type ImageSpec struct {
//+kubebuilder:validation:Required
Name ImageName `json:"name,omitempty"`
Type ImageType `json:"type,omitempty"`
DetailInfo ImageDetailInfo `json:"detail,omitempty"`
}
@@ -162,6 +170,21 @@ func (i *Image) getName() string {
return i.Name
}
func (i *Image) MulateFromOldobj(old *Image) {
if i.Spec.DetailInfo.Docs == "" {
i.Spec.DetailInfo.Docs = old.Spec.DetailInfo.Docs
}
if i.Spec.DetailInfo.Icon == "" {
i.Spec.DetailInfo.Icon = old.Spec.DetailInfo.Icon
}
if i.Spec.DetailInfo.Description == "" {
i.Spec.DetailInfo.Description = old.Spec.DetailInfo.Description
}
if len(i.Spec.DetailInfo.Keywords) == 0 {
i.Spec.DetailInfo.Keywords = old.Spec.DetailInfo.Keywords
}
}
//+kubebuilder:object:root=true
// ImageList contains a list of Image
+12 -1
View File
@@ -35,7 +35,7 @@ import (
var imagelog = logf.Log.WithName("image-resource")
func (i *Image) SetupWebhookWithManager(mgr ctrl.Manager) error {
m := &ImageMutator{}
m := &ImageMutator{Client: mgr.GetClient()}
v := &ImageValidator{Client: mgr.GetClient()}
return ctrl.NewWebhookManagedBy(mgr).
For(i).
@@ -45,8 +45,10 @@ func (i *Image) SetupWebhookWithManager(mgr ctrl.Manager) error {
}
//+kubebuilder:webhook:path=/mutate-imagehub-sealos-io-v1-image,mutating=true,failurePolicy=fail,sideEffects=None,groups=imagehub.sealos.io,resources=images,verbs=create;update,versions=v1,name=mimage.kb.io,admissionReviewVersions=v1
//+kubebuilder:object:generate=false
type ImageMutator struct {
client.Client
}
func (m *ImageMutator) Default(ctx context.Context, obj runtime.Object) error {
@@ -59,6 +61,15 @@ func (m *ImageMutator) Default(ctx context.Context, obj runtime.Object) error {
img.ObjectMeta.Labels[SealosOrgLable] = img.Spec.Name.GetOrg()
img.ObjectMeta.Labels[SealosRepoLabel] = img.Spec.Name.GetRepo()
img.ObjectMeta.Labels[SealosTagLabel] = img.Spec.Name.GetTag()
oldimg := &Image{}
oldimg.Name = img.Name
err := m.Get(ctx, client.ObjectKeyFromObject(oldimg), oldimg)
if err != nil {
return client.IgnoreNotFound(err)
}
// mulate image cr
img.MulateFromOldobj(oldimg)
return nil
}
@@ -371,21 +371,6 @@ func (in *ImageList) DeepCopyObject() runtime.Object {
return nil
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *ImageMutator) DeepCopyInto(out *ImageMutator) {
*out = *in
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ImageMutator.
func (in *ImageMutator) DeepCopy() *ImageMutator {
if in == nil {
return nil
}
out := new(ImageMutator)
in.DeepCopyInto(out)
return out
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *ImageSpec) DeepCopyInto(out *ImageSpec) {
*out = *in
@@ -110,6 +110,8 @@ spec:
- size
type: object
type: array
type:
type: string
type: object
description: Datas in datapack status
type: object
@@ -80,6 +80,8 @@ spec:
type: object
name:
type: string
type:
type: string
type: object
status:
description: ImageStatus defines the observed state of Image
@@ -132,6 +132,8 @@ spec:
- size
type: object
type: array
type:
type: string
type: object
description: Datas in datapack status
type: object
@@ -241,6 +243,8 @@ spec:
type: object
name:
type: string
type:
type: string
type: object
status:
description: ImageStatus defines the observed state of Image