Skip to content
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

Trigger acl-operator for tsuru jobs #10

Merged
merged 3 commits into from
Sep 20, 2023
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
112 changes: 105 additions & 7 deletions engine/operator/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/sirupsen/logrus"
"github.com/tsuru/acl-api/api/types"
"github.com/tsuru/acl-api/engine"
"github.com/tsuru/acl-api/kubernetes"
aclKube "github.com/tsuru/acl-api/kubernetes"
"github.com/tsuru/acl-api/rule"
k8sErrors "k8s.io/apimachinery/pkg/api/errors"
Expand Down Expand Up @@ -52,13 +51,21 @@ func (e *ACLOperatorEngine) AfterSync() error {
}

func (e *ACLOperatorEngine) Sync(r types.Rule) (interface{}, error) {
ctx := context.TODO()
log := logger.WithField("ruleid", r.RuleID)
if r.Source.TsuruApp != nil {
return e.SyncApp(r)
}

if r.Source.TsuruApp == nil {
return nil, nil
if r.Source.TsuruJob != nil {
return e.SyncJob(r)
}

return nil, nil
}

func (e *ACLOperatorEngine) SyncApp(r types.Rule) (interface{}, error) {
ctx := context.TODO()
log := logger.WithField("ruleid", r.RuleID)

source, err := e.logicCache.LogicFromRule(r)
if err != nil {
return nil, err
Expand All @@ -67,7 +74,7 @@ func (e *ACLOperatorEngine) Sync(r types.Rule) (interface{}, error) {
return nil, nil
}

restConfig, err := source.KubernetesRestConfig()
restConfig, _, err := source.KubernetesRestConfig()
if err != nil {
return nil, err
}
Expand All @@ -77,7 +84,7 @@ func (e *ACLOperatorEngine) Sync(r types.Rule) (interface{}, error) {
return nil, nil
}

tsuruClient, err := kubernetes.GetTsuruClientWithRestConfig(restConfig)
tsuruClient, err := aclKube.GetTsuruClientWithRestConfig(restConfig)
if err != nil {
return "", err
}
Expand Down Expand Up @@ -145,3 +152,94 @@ func (e *ACLOperatorEngine) Sync(r types.Rule) (interface{}, error) {

return "triggered acl-operator in the last minute", nil
}

func (e *ACLOperatorEngine) SyncJob(r types.Rule) (interface{}, error) {
ctx := context.TODO()
log := logger.WithField("ruleid", r.RuleID)

source, err := e.logicCache.LogicFromRule(r)
if err != nil {
return nil, err
}

if source == nil {
return nil, nil
}

restConfig, pool, err := source.KubernetesRestConfig()
if err != nil {
return nil, err
}

if restConfig == nil {
log.Debugf("Ignoring rule, not a kubernetes source")
return nil, nil
}

k8sClient, err := aclKube.GetClientWithRestConfig(restConfig)
if err != nil {
return "", err
}

tsuruJobName := r.Source.TsuruJob.JobName
cronJobNamespace := k8sClient.BatchV1().CronJobs("tsuru-" + pool)
cronJobCRD, err := cronJobNamespace.Get(ctx, tsuruJobName, metav1.GetOptions{})
if err != nil {
if k8sErrors.IsNotFound(err) {
return "", nil
}
return "", err
}

originalData, err := json.Marshal(cronJobCRD)
if err != nil {
return "", err
}

lastUpdatedStr := cronJobCRD.Annotations[lastUpdatedAnnotation]
var lastUpdated time.Time
needsUpdate := false

if lastUpdatedStr == "" {
needsUpdate = true
} else {
lastUpdated, err = time.Parse(time.RFC3339, lastUpdatedStr)
if err != nil {
return "", err
}

if r.Created.UTC().Add(time.Minute).After(lastUpdated) {
needsUpdate = true
}

if time.Now().UTC().After(lastUpdated.Add(time.Minute)) {
needsUpdate = true
}
}

if needsUpdate {
if cronJobCRD.Annotations == nil {
cronJobCRD.Annotations = map[string]string{}
}
cronJobCRD.Annotations[lastUpdatedAnnotation] = time.Now().UTC().Format(time.RFC3339)

updatedData, err := json.Marshal(cronJobCRD)
if err != nil {
return "", err
}

data, err := jsonpatch.CreateMergePatch(originalData, updatedData)
if err != nil {
return "", err
}

_, err = cronJobNamespace.Patch(ctx, tsuruJobName, k8sTypes.MergePatchType, data, metav1.PatchOptions{})
if err != nil {
return "", err
}

return "triggered acl-operator", nil
}

return "triggered acl-operator in the last minute", nil
}
Loading
Loading