Skip to content

chore: Add new row for CRD Images in get kubectl cmd #69

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/69.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:chore
`chore` - Get crd image status information.
```
34 changes: 34 additions & 0 deletions api/v1alpha1/image_models.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package v1alpha1

type (
ImageStatusLastSync string
)

var (
// ImageStatusLastSyncSuccess is the status of the image when it is last sync success.
ImageStatusLastSyncSuccess ImageStatusLastSync = "Success"

// ImageStatusLastSyncScheduled is the status of the image when it is last sync is scheduled.
ImageStatusLastSyncScheduled ImageStatusLastSync = "Scheduled"

// ImageStatusError is the status of the image when an error occurred.
ImageStatusLastSyncError ImageStatusLastSync = "Error"

// ImageStatusLastSyncGetError is the status of the image when it is last sync get error.
ImageStatusLastSyncErrorGetImage ImageStatusLastSync = "GetImageError"

// ImageStatusLastSyncErrorSecrets is the status of the image when it is last sync error secrets.
ImageStatusLastSyncErrorPullSecrets ImageStatusLastSync = "PullSecretsError"

// ImageStatusLastSyncErrorRegistry is the status of the image when it is last sync error registry.
ImageStatusLastSyncErrorRegistry ImageStatusLastSync = "RegistryError"

// ImageStatusLastSyncErrorTags is the status of the image when it is last sync error tags.
ImageStatusLastSyncErrorTags ImageStatusLastSync = "TagsError"

// ImageStatusLastSyncErrorGetRule is the status of the image when it is last sync error get rule.
ImageStatusLastSyncErrorGetRule ImageStatusLastSync = "GetRuleError"

// ImageStatusLastSyncErrorAction is the status of the image when it is last sync error action.
ImageStatusLastSyncErrorAction ImageStatusLastSync = "ActionError"
)
16 changes: 15 additions & 1 deletion api/v1alpha1/image_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,9 @@ type (
ImageStatus struct {
// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
// Important: Run "make" to regenerate code after modifying this file
Tag string `json:"tag"`
Tag string `json:"tag"`
Result ImageStatusLastSync `json:"result"`
Time string `json:"time"`
}
)

Expand All @@ -111,6 +113,8 @@ type (
// Image is the Schema for the images API
// +kubebuilder:printcolumn:name="Image",type=string,JSONPath=`.spec.image`
// +kubebuilder:printcolumn:name="Tag",type=string,JSONPath=`.status.tag`
// +kubebuilder:printcolumn:name="Last-Result",type=string,JSONPath=`.status.result`
// +kubebuilder:printcolumn:name="Last-Sync",type=date,JSONPath=`.status.time`
type Image struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`
Expand All @@ -137,6 +141,16 @@ func (i *Image) SetStatusTag(tag string) {
i.Status.Tag = tag
}

// SetStatusExecution sets the status execution of the image
func (i *Image) SetStatusResult(result ImageStatusLastSync) {
i.Status.Result = result
}

// SetStatusTime sets the status time of the image
func (i *Image) SetStatusTime(time string) {
i.Status.Time = time
}

// GetImageWithTag returns the image name with the tag
func (i *Image) GetImageWithTag() string {
if i.Status.Tag == "" {
Expand Down
2 changes: 1 addition & 1 deletion cmd/admission-controller/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var (
insideCluster bool = true // running inside k8s cluster

webhookNamespace string = "nip.io"
webhookServiceName string = "192-168-1-23"
webhookServiceName string = "192-168-1-30"
webhookConfigName string = "mutating-webhook-configuration"
webhookPathMutate string = "/mutate"
webhookPort string = ":8443"
Expand Down
20 changes: 19 additions & 1 deletion cmd/kimup/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
log "github.com/sirupsen/logrus"
"k8s.io/client-go/util/retry"

"github.com/orange-cloudavenue/kube-image-updater/api/v1alpha1"
"github.com/orange-cloudavenue/kube-image-updater/internal/actions"
"github.com/orange-cloudavenue/kube-image-updater/internal/kubeclient"
"github.com/orange-cloudavenue/kube-image-updater/internal/metrics"
Expand Down Expand Up @@ -53,7 +54,20 @@ func initScheduler(ctx context.Context, k kubeclient.Interface) {
defer cancel()

image, err := k.Image().Get(ctx, e.Data()["namespace"].(string), e.Data()["image"].(string))
defer func() {
// update the status of the image
image.SetStatusTime(time.Now().Format(time.RFC3339))
err := k.Image().UpdateStatus(ctx, image)
if err != nil {
log.WithError(err).
WithFields(log.Fields{
"Namespace": e.Data()["namespace"],
"Image": e.Data()["image"],
}).Error("Error updating status of image")
}
}()
if err != nil {
image.SetStatusResult(v1alpha1.ImageStatusLastSyncErrorGetImage)
if err := crontab.RemoveJob(crontab.BuildKey(e.Data()["namespace"].(string), e.Data()["image"].(string))); err != nil {
return err
}
Expand All @@ -64,6 +78,7 @@ func initScheduler(ctx context.Context, k kubeclient.Interface) {

if image.Spec.ImagePullSecrets != nil {
auths, err = k.GetPullSecretsForImage(ctx, image)
image.SetStatusResult(v1alpha1.ImageStatusLastSyncErrorPullSecrets)
if err != nil {
return err
}
Expand Down Expand Up @@ -94,7 +109,7 @@ func initScheduler(ctx context.Context, k kubeclient.Interface) {
if err != nil {
// Prometheus metrics - Increment the counter for the registry with error
metrics.Registry().RequestErrorTotal.WithLabelValues(i.GetRegistry()).Inc()

image.SetStatusResult(v1alpha1.ImageStatusLastSyncErrorRegistry)
return err
}

Expand All @@ -117,6 +132,7 @@ func initScheduler(ctx context.Context, k kubeclient.Interface) {
for _, rule := range image.Spec.Rules {
r, err := rules.GetRule(rule.Type)
if err != nil {
image.SetStatusResult(v1alpha1.ImageStatusLastSyncErrorGetRule)
log.Errorf("Error getting rule: %v", err)
continue
}
Expand Down Expand Up @@ -148,6 +164,7 @@ func initScheduler(ctx context.Context, k kubeclient.Interface) {
if match {
for _, action := range rule.Actions {
a, err := actions.GetActionWithUntypedName(action.Type)
image.SetStatusResult(v1alpha1.ImageStatusLastSyncErrorAction)
if err != nil {
log.Errorf("Error getting action: %v", err)
continue
Expand Down Expand Up @@ -180,6 +197,7 @@ func initScheduler(ctx context.Context, k kubeclient.Interface) {
}
}

image.SetStatusResult(v1alpha1.ImageStatusLastSyncSuccess)
return k.Image().Update(ctx, image)
})

Expand Down
12 changes: 12 additions & 0 deletions config/crd/bases/kimup.cloudavenue.io_images.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ spec:
- jsonPath: .status.tag
name: Tag
type: string
- jsonPath: .status.result
name: Last-Result
type: string
- jsonPath: .status.time
name: Last-Sync
type: date
name: v1alpha1
schema:
openAPIV3Schema:
Expand Down Expand Up @@ -213,13 +219,19 @@ spec:
status:
description: ImageStatus defines the observed state of Image
properties:
result:
type: string
tag:
description: |-
INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
Important: Run "make" to regenerate code after modifying this file
type: string
time:
type: string
required:
- result
- tag
- time
type: object
type: object
served: true
Expand Down
Loading