blob: dd7dcfb7ac364cd48546e8192b4a88e916f123b6 [file] [log] [blame]
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package crdclient
import (
"context"
"fmt"
dubbo_apache_org_v1alpha1 "github.com/apache/dubbo-kubernetes/api/resource/v1alpha1"
"github.com/apache/dubbo-kubernetes/pkg/core/gen/apis/dubbo.apache.org/v1alpha1"
"github.com/apache/dubbo-kubernetes/pkg/core/gen/generated/clientset/versioned"
"github.com/apache/dubbo-kubernetes/pkg/core/model"
"github.com/apache/dubbo-kubernetes/pkg/core/schema/collections"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
)
func create(ic versioned.Interface, cfg model.Config, objMeta metav1.ObjectMeta) (metav1.Object, error) {
switch cfg.GroupVersionKind {
{{- range . }}
case collections.{{ .VariableName }}.Resource().GroupVersionKind():
return {{.Client}}.{{ .ClientGroupPath }}().{{ .ClientTypePath }}({{if .Namespaced}}cfg.Namespace{{end}}).Create(context.TODO(), &{{ .ClientImport }}.{{ .Kind }}{
ObjectMeta: objMeta,
Spec: *(cfg.Spec.(*{{ .APIImport }}.{{ .Kind }}{{ .TypeSuffix }})),
}, metav1.CreateOptions{})
{{- end }}
default:
return nil, fmt.Errorf("unsupported type: %v", cfg.GroupVersionKind)
}
}
func update(ic versioned.Interface, cfg model.Config, objMeta metav1.ObjectMeta) (metav1.Object, error) {
switch cfg.GroupVersionKind {
{{- range . }}
case collections.{{ .VariableName }}.Resource().GroupVersionKind():
return {{.Client}}.{{ .ClientGroupPath }}().{{ .ClientTypePath }}({{if .Namespaced}}cfg.Namespace{{end}}).Update(context.TODO(), &{{ .ClientImport }}.{{ .Kind }}{
ObjectMeta: objMeta,
Spec: *(cfg.Spec.(*{{ .APIImport }}.{{ .Kind }}{{ .TypeSuffix }})),
}, metav1.UpdateOptions{})
{{- end }}
default:
return nil, fmt.Errorf("unsupported type: %v", cfg.GroupVersionKind)
}
}
func delete(ic versioned.Interface, typ model.GroupVersionKind, name, namespace string, resourceVersion *string) error {
var deleteOptions metav1.DeleteOptions
if resourceVersion != nil {
deleteOptions.Preconditions = &metav1.Preconditions{ResourceVersion: resourceVersion}
}
switch typ {
{{- range . }}
case collections.{{ .VariableName }}.Resource().GroupVersionKind():
return {{.Client}}.{{ .ClientGroupPath }}().{{ .ClientTypePath }}({{if .Namespaced}}namespace{{end}}).Delete(context.TODO(), name, deleteOptions)
{{- end }}
default:
return fmt.Errorf("unsupported type: %v", typ)
}
}
var translationMap = map[model.GroupVersionKind]func(r runtime.Object) *model.Config{
{{- range . }}
collections.{{ .VariableName }}.Resource().GroupVersionKind(): func(r runtime.Object) *model.Config {
obj := r.(*{{ .ClientImport }}.{{ .Kind }})
return &model.Config{
Meta: model.Meta{
GroupVersionKind: collections.{{ .VariableName }}.Resource().GroupVersionKind(),
Name: obj.Name,
Namespace: obj.Namespace,
Labels: obj.Labels,
Annotations: obj.Annotations,
ResourceVersion: obj.ResourceVersion,
CreationTimestamp: obj.CreationTimestamp.Time,
OwnerReferences: obj.OwnerReferences,
UID: string(obj.UID),
Generation: obj.Generation,
},
Spec: &obj.Spec,
}
},
{{- end }}
}