Add the update method

This commit is contained in:
Nikolai Rodionov 2024-04-30 11:19:56 +02:00
parent 84819c5842
commit a95cf4a436
Signed by: allanger
GPG Key ID: 0AA46A90E25592AD
3 changed files with 116 additions and 1 deletions

View File

@ -71,7 +71,51 @@ func (e *EnvironmentsServer) Create(ctx context.Context, in *proto.CreateOptions
} }
func (e *EnvironmentsServer) Update(ctx context.Context, in *proto.UpdateOptions) (*proto.EnvironmentFull, error) { func (e *EnvironmentsServer) Update(ctx context.Context, in *proto.UpdateOptions) (*proto.EnvironmentFull, error) {
return nil, nil provider, err := providers.GetProvider(in.GetSpec().GetProvider().String())
if err != nil {
return nil, err
}
k8s, err := kubernetes.GetKubernetes(in.GetSpec().GetKubernetes().String())
if err != nil {
return nil, err
}
location, err := provider.GetServerLocation(in.GetSpec().GetServerLocation().String())
if err != nil {
return nil, err
}
serverType, err := provider.GetServerType(in.Spec.ServerType.String())
if err != nil {
return nil, err
}
data := &controllers.EnvironemntData{
Name: in.GetMetadata().GetName(),
Description: in.GetMetadata().GetDescription(),
Provider: provider.GetProviderName(),
Kubernetes: k8s.GetKubernetesName(),
Location: location,
ServerType: serverType,
}
environment := &controllers.Environemnt{
UserID: in.GetOwnerId().GetUuid(),
Controller: e.controller,
Data: data,
Token: in.GetToken().GetToken(),
}
err = environment.Update(ctx)
if err != nil {
return nil, err
}
return &proto.EnvironmentFull{
Metadata: in.GetMetadata(),
Spec: in.GetSpec(),
}, nil
} }
func (e *EnvironmentsServer) Delete(ctx context.Context, in *proto.DeleteOptions) (*empty.Empty, error) { func (e *EnvironmentsServer) Delete(ctx context.Context, in *proto.DeleteOptions) (*empty.Empty, error) {

View File

@ -112,6 +112,70 @@ func (env *Environemnt) Create(ctx context.Context) error {
return nil return nil
} }
func (env *Environemnt) Update(ctx context.Context) error {
if err := env.isNsVerified(ctx); err != nil {
log.Println("Can't verify ns")
return err
}
env.Controller.GetClient()
conf := &rest.Config{
Host: "https://kubernetes.default.svc.cluster.local:443",
BearerToken: env.Token,
TLSClientConfig: rest.TLSClientConfig{
Insecure: true,
},
}
controller, err := ctrl.NewManager(conf, ctrl.Options{})
if err != nil {
return err
}
oldEnv := &Environemnt{
Controller: env.Controller,
UserID: env.UserID,
Token: env.Token,
}
if err := oldEnv.Get(ctx); err != nil {
return err
}
// Check whter immutable fields are changed
if oldEnv.Data.Provider != env.Data.Provider {
return errors.New("provider can't be changed")
}
if oldEnv.Data.Location != env.Data.Location {
return errors.New("location can't be changed")
}
vars, err := env.Data.buildVars()
if err != nil {
return err
}
obj := corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: env.Data.Name,
Namespace: env.UserID,
Labels: map[string]string{
"component": "bootstrap",
"kind": "environment",
},
},
Data: map[string]string{
"description": env.Data.Description,
"vars": vars,
},
}
if err := kube.Update(ctx, controller.GetClient(), &obj); err != nil {
return err
}
return nil
}
func (env *Environemnt) Delete(ctx context.Context) error { func (env *Environemnt) Delete(ctx context.Context) error {
env.Controller.GetClient() env.Controller.GetClient()
conf := &rest.Config{ conf := &rest.Config{

View File

@ -22,6 +22,13 @@ func Create(ctx context.Context, client client.Client, obj client.Object, wait b
return nil return nil
} }
func Update(ctx context.Context, client client.Client, obj client.Object) error {
if err := client.Update(ctx, obj); err != nil {
return err
}
return nil
}
func SetOwnerRef(ctx context.Context, client client.Client, obj client.Object, owner client.Object) client.Object { func SetOwnerRef(ctx context.Context, client client.Client, obj client.Object, owner client.Object) client.Object {
apiVersion := fmt.Sprintf("%s/%s", owner.GetObjectKind().GroupVersionKind().Group, owner.GetObjectKind().GroupVersionKind().Version) apiVersion := fmt.Sprintf("%s/%s", owner.GetObjectKind().GroupVersionKind().Group, owner.GetObjectKind().GroupVersionKind().Version)
ownerReference := []metav1.OwnerReference{ ownerReference := []metav1.OwnerReference{