-
Notifications
You must be signed in to change notification settings - Fork 17
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
Feat :support create service #46
base: main
Are you sure you want to change the base?
Changes from 1 commit
9775e3f
e015708
1672387
1fc1e40
d94dbc3
5dbf380
ed3b9a6
d7f75b1
cc9781b
c90ef38
6b882f6
40dc64f
64956ef
2889aab
dddc1e5
25c23fd
729aaa3
4e33ba3
aea9af9
919a3cb
e2eeb4d
5d0c438
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,7 +20,6 @@ import ( | |
"context" | ||
"fmt" | ||
|
||
"cuelang.org/go/cue" | ||
"github.com/crossplane/crossplane-runtime/pkg/meta" | ||
"github.com/pkg/errors" | ||
"github.com/sirupsen/logrus" | ||
|
@@ -35,6 +34,8 @@ import ( | |
ctrl "sigs.k8s.io/controller-runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
|
||
"cuelang.org/go/cue" | ||
|
||
"github.com/kubevela/pkg/cue/cuex" | ||
"github.com/kubevela/pkg/util/slices" | ||
"github.com/kubevela/pkg/util/template/definition" | ||
|
@@ -91,25 +92,36 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu | |
return ctrl.Result{}, err | ||
} | ||
|
||
deploymentList := &appsv1.DeploymentList{} | ||
if err := r.List(ctx, deploymentList, client.MatchingLabels(map[string]string{ | ||
triggerNameLabel: ts.Name, | ||
})); err != nil { | ||
if err := r.handleWorker(ctx, ts); err != nil { | ||
logger.Error(err, "failed to handle trigger worker") | ||
return ctrl.Result{}, err | ||
} | ||
// if no worker deployment exists, create a new one to run trigger | ||
if len(deploymentList.Items) == 0 { | ||
if err := r.createWorker(ctx, ts); err != nil { | ||
logger.Error(err, "failed to create worker deployment for trigger") | ||
return ctrl.Result{}, err | ||
} | ||
logger.Info("successfully create worker deployment for trigger") | ||
return ctrl.Result{}, nil | ||
} | ||
|
||
func (r *Reconciler) handleWorker(ctx context.Context, ts *standardv1alpha1.TriggerService) error { | ||
|
||
v, err := r.loadTemplateCueValue(ctx, ts) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return ctrl.Result{}, nil | ||
if err := r.createRoles(ctx, ts, v); err != nil { | ||
return err | ||
} | ||
|
||
if err := r.createDeployment(ctx, ts, v); err != nil { | ||
return err | ||
} | ||
|
||
if err := r.createService(ctx, ts, v); err != nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please check There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you mean check before invoking the function? or we can check the value in the function ? |
||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (r *Reconciler) createWorker(ctx context.Context, ts *standardv1alpha1.TriggerService) error { | ||
func (r *Reconciler) loadTemplateCueValue(ctx context.Context, ts *standardv1alpha1.TriggerService) (cue.Value, error) { | ||
templateName := "default" | ||
opts := make([]cuex.CompileOption, 0) | ||
opts = append(opts, cuex.WithExtraData("triggerService", map[string]string{ | ||
|
@@ -124,25 +136,38 @@ func (r *Reconciler) createWorker(ctx context.Context, ts *standardv1alpha1.Trig | |
} | ||
template, err := definition.NewTemplateLoader(ctx, r.Client).LoadTemplate(ctx, templateName, definition.WithType(triggertypes.DefinitionTypeTriggerWorker)) | ||
if err != nil { | ||
return err | ||
return cue.Value{}, err | ||
} | ||
|
||
v, err := cuex.CompileStringWithOptions(ctx, template.Compile(), opts...) | ||
if err != nil { | ||
return err | ||
} | ||
if err := r.createRoles(ctx, ts, v); err != nil { | ||
return err | ||
return cue.Value{}, err | ||
} | ||
|
||
return v, nil | ||
} | ||
|
||
func (r *Reconciler) createDeployment(ctx context.Context, ts *standardv1alpha1.TriggerService, v cue.Value) error { | ||
data, err := v.LookupPath(cue.ParsePath("deployment")).MarshalJSON() | ||
if err != nil { | ||
return err | ||
} | ||
deploy := &appsv1.Deployment{} | ||
if err := json.Unmarshal(data, deploy); err != nil { | ||
|
||
expectedDeployment := new(appsv1.Deployment) | ||
if err := json.Unmarshal(data, expectedDeployment); err != nil { | ||
return err | ||
} | ||
utils.SetOwnerReference(deploy, ts) | ||
return r.Create(ctx, deploy) | ||
|
||
existDeployment := new(appsv1.Deployment) | ||
if err := r.Get(ctx, types.NamespacedName{Namespace: ts.Namespace, Name: ts.Name}, existDeployment); err != nil { | ||
if apierrors.IsNotFound(err) { | ||
utils.SetOwnerReference(expectedDeployment, ts) | ||
return r.Create(ctx, expectedDeployment) | ||
} | ||
return err | ||
} | ||
|
||
return r.Patch(ctx, expectedDeployment, client.Merge) | ||
} | ||
|
||
func (r *Reconciler) createRoles(ctx context.Context, ts *standardv1alpha1.TriggerService, v cue.Value) error { | ||
|
@@ -183,6 +208,31 @@ func (r *Reconciler) createRoles(ctx context.Context, ts *standardv1alpha1.Trigg | |
return nil | ||
} | ||
|
||
func (r *Reconciler) createService(ctx context.Context, ts *standardv1alpha1.TriggerService, v cue.Value) error { | ||
data, err := v.LookupPath(cue.ParsePath("service")).MarshalJSON() | ||
if err != nil { | ||
return err | ||
} | ||
if data == nil { | ||
return nil | ||
} | ||
|
||
expectSvc := new(corev1.Service) | ||
if err := json.Unmarshal(data, expectSvc); err != nil { | ||
return err | ||
} | ||
|
||
existSvc := new(corev1.Service) | ||
if err := r.Client.Get(ctx, types.NamespacedName{Namespace: ts.Namespace, Name: ts.Name}, existSvc); err != nil { | ||
if apierrors.IsNotFound(err) { | ||
utils.SetOwnerReference(expectSvc, ts) | ||
return r.Client.Create(ctx, expectSvc) | ||
} | ||
return err | ||
} | ||
return r.Client.Patch(ctx, expectSvc, client.Merge) | ||
} | ||
|
||
func (r *Reconciler) handleTriggerConfig(ctx context.Context, ts *standardv1alpha1.TriggerService) error { | ||
// Add TriggerService into ConfigMap | ||
jsonByte, err := json.Marshal(ts.Spec) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why users need to specify the clusterIP?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.