Skip to content

Commit

Permalink
feat: add base of alert and refacto kubeclient (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
azrod authored Oct 3, 2024
1 parent a52ec04 commit 692e078
Show file tree
Hide file tree
Showing 17 changed files with 879 additions and 181 deletions.
9 changes: 9 additions & 0 deletions api/v1alpha1/alert.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package v1alpha1

type (
AlertCoreSpec struct{}

AlertCoreStatus struct {
Synced bool `json:"synced"`
}
)
50 changes: 50 additions & 0 deletions api/v1alpha1/alert_discord_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package v1alpha1

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

type (
// AlertDiscordSpec defines the desired state of AlertDiscord
AlertDiscordSpec struct {
// +kubebuilder:validation:Required
// +kubebuilder:validation:MinItems=1
// +kubebuilder:validation:example:="123456789012345678"
ChannelIDs []string `json:"channelIDs"`

// +kubebuilder:validation:Optional
CredentialBotToken *ValueOrValueFrom `json:"credentialBotToken,omitempty"`

// +kubebuilder:validation:Optional
CredentialOAuth2Token *ValueOrValueFrom `json:"credentialOAuth2,omitempty"`
}

// AlertDiscordStatus defines the observed state of AlertDiscord
AlertDiscordStatus struct {
AlertCoreStatus `json:",inline"`
}
)

// +kubebuilder:object:root=true
// +kubebuilder:subresource:status
// +kubebuilder:resource:path=alertdiscords,scope=Cluster

type AlertDiscord struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

Spec AlertDiscordSpec `json:"spec,omitempty"`
Status AlertDiscordStatus `json:"status,omitempty"`
}

// +kubebuilder:object:root=true

type AlertDiscordList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []AlertDiscord `json:"items"`
}

func init() {
// SchemeBuilder.Register(&AlertDiscord{}, &AlertDiscordList{})
}
78 changes: 78 additions & 0 deletions api/v1alpha1/value_or_valueFrom.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package v1alpha1

import (
corev1 "k8s.io/api/core/v1"
)

type (
ValueOrValueFrom struct {
// Value is a string value to assign to the key.
// if ValueFrom is specified, this value is ignored.
// +optional
Value string `json:"value,omitempty"`

// ValueFrom is a reference to a field in a secret or config map.
// +optional
ValueFrom *ValueFromSource `json:"valueFrom,omitempty"`
}

// ValueFromSource is a reference to a field in a secret or config map.
ValueFromSource struct {
// SecretKeyRef is a reference to a field in a secret.
// +optional
SecretKeyRef *corev1.SecretKeySelector `json:"secretKeyRef,omitempty"`

// ConfigMapKeyRef is a reference to a field in a config map.
// +optional
ConfigMapKeyRef *corev1.ConfigMapKeySelector `json:"configMapKeyRef,omitempty"`
}
)

// func (v *ValueOrValueFrom) GetValue(ctx context.Context, namespace, name string) (string, error) {

// // If ValueFrom is nil, return the value
// if v.ValueFrom == nil {
// return v.Value, nil
// }

// if v.ValueFrom.SecretKeyRef == nil && v.ValueFrom.ConfigMapKeyRef == nil {
// return "", fmt.Errorf("ValueFrom is specified but neither SecretKeyRef nor ConfigMapKeyRef is set")
// }

// // // Read from config map
// // if p.ConfigMapKeyRef != nil {
// // var configMap coreV1.ConfigMap
// // objectKey := types.NamespacedName{Namespace: namespace, Name: p.ConfigMapKeyRef.Name}
// // err := r.GetKubeClient().Get(ctx, objectKey, &configMap)
// // if errors.IsNotFound(err) {
// // return "", err
// // }
// // key := p.ConfigMapKeyRef.Key
// // if value, found := configMap.Data[key]; found {
// // return value, nil
// // }
// // return "", brose_errors.NewMapEntryNotFoundError(key, nil)
// // }

// // // Read from secret
// // if p.SecretKeyRef != nil {
// // var secret coreV1.Secret
// // objectKey := types.NamespacedName{Namespace: namespace, Name: p.SecretKeyRef.Name}
// // err := r.Get(ctx, objectKey, &secret)
// // if errors.IsNotFound(err) {
// // return "", err
// // }
// // key := p.SecretKeyRef.Key
// // valueBase64, foundBase64 := secret.Data[key]
// // valueString, foundString := secret.StringData[key]
// // if !foundBase64 && !foundString {
// // return "", brose_errors.NewMapEntryNotFoundError(key, nil)
// // } else if foundString {
// // return valueString, nil
// // }
// // return string(valueBase64), nil
// // }
// // return "", brose_errors.NewMissingPropertyValueError(name, nil)

// return "", nil
// }
180 changes: 180 additions & 0 deletions api/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 11 additions & 11 deletions cmd/kimup/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func main() {
initScheduler(ctx, k)

go func() {
x, err := k.WatchEventsImage(ctx)
x, err := k.Image().Watch(ctx)
if err != nil {
log.Panicf("Error watching events: %v", err)
}
Expand All @@ -65,20 +65,20 @@ func main() {
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()

an := annotations.New(ctx, event.Value)
an := annotations.New(ctx, &event.Value)

switch event.Type {
case "ADDED":
// Clean old action
an.Remove(annotations.KeyAction)

setupTriggers(event.Value)
refreshIfRequired(an, *event.Value)
if err := setTagIfNotExists(ctx, an, event.Value); err != nil {
setupTriggers(&event.Value)
refreshIfRequired(an, event.Value)
if err := setTagIfNotExists(ctx, an, &event.Value); err != nil {
log.Errorf("Error setting tag: %v", err)
}

if err := k.SetImage(ctx, *event.Value); err != nil {
if err := k.Image().Update(ctx, event.Value); err != nil {
log.Errorf("Error updating image: %v", err)
}

Expand All @@ -90,7 +90,7 @@ func main() {
for _, trigger := range event.Value.Spec.Triggers {
switch trigger.Type {
case triggers.Crontab:
cleanTriggers(event.Value)
cleanTriggers(&event.Value)
case triggers.Webhook:
log.Info("Webhook trigger not implemented yet")
}
Expand All @@ -100,16 +100,16 @@ func main() {
an.Remove(annotations.KeyAction)
}

refreshIfRequired(an, *event.Value)
refreshIfRequired(an, event.Value)

if err := k.SetImage(ctx, *event.Value); err != nil {
if err := k.Image().Update(ctx, event.Value); err != nil {
log.Errorf("Error updating image: %v", err)
}

setupTriggers(event.Value)
setupTriggers(&event.Value)

case "DELETED":
cleanTriggers(event.Value)
cleanTriggers(&event.Value)
}
}
}
Expand Down
Loading

0 comments on commit 692e078

Please sign in to comment.