Add the update method
This commit is contained in:
parent
84819c5842
commit
a95cf4a436
@ -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) {
|
||||
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) {
|
||||
|
@ -112,6 +112,70 @@ func (env *Environemnt) Create(ctx context.Context) error {
|
||||
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 {
|
||||
env.Controller.GetClient()
|
||||
conf := &rest.Config{
|
||||
|
@ -22,6 +22,13 @@ func Create(ctx context.Context, client client.Client, obj client.Object, wait b
|
||||
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 {
|
||||
apiVersion := fmt.Sprintf("%s/%s", owner.GetObjectKind().GroupVersionKind().Group, owner.GetObjectKind().GroupVersionKind().Version)
|
||||
ownerReference := []metav1.OwnerReference{
|
||||
|
Loading…
Reference in New Issue
Block a user