|
|
|
@ -1,13 +1,11 @@
|
|
|
|
|
package infrastructure
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
|
|
|
|
"context"
|
|
|
|
|
"encoding/base64"
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"errors"
|
|
|
|
|
"fmt"
|
|
|
|
|
"html/template"
|
|
|
|
|
"oc-datacenter/conf"
|
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
|
@ -16,12 +14,40 @@ import (
|
|
|
|
|
rbacv1 "k8s.io/api/rbac/v1"
|
|
|
|
|
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
|
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
|
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
|
|
|
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
|
|
|
|
apply "k8s.io/client-go/applyconfigurations/core/v1"
|
|
|
|
|
"k8s.io/client-go/dynamic"
|
|
|
|
|
"k8s.io/client-go/kubernetes"
|
|
|
|
|
"k8s.io/client-go/rest"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var gvrSources = schema.GroupVersionResource{Group: "multicluster.admiralty.io", Version: "v1alpha1", Resource: "sources"}
|
|
|
|
|
var gvrTargets = schema.GroupVersionResource{Group: "multicluster.admiralty.io", Version: "v1alpha1", Resource: "targets"}
|
|
|
|
|
|
|
|
|
|
type KubernetesService struct {
|
|
|
|
|
Set *kubernetes.Clientset
|
|
|
|
|
Set *kubernetes.Clientset
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewDynamicClient() (*dynamic.DynamicClient, error) {
|
|
|
|
|
config := &rest.Config{
|
|
|
|
|
Host: conf.GetConfig().KubeHost + ":" + conf.GetConfig().KubePort,
|
|
|
|
|
TLSClientConfig: rest.TLSClientConfig{
|
|
|
|
|
CAData: []byte(conf.GetConfig().KubeCA),
|
|
|
|
|
CertData: []byte(conf.GetConfig().KubeCert),
|
|
|
|
|
KeyData: []byte(conf.GetConfig().KubeData),
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dynamicClient, err := dynamic.NewForConfig(config)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, errors.New("Error creating Dynamic client: " + err.Error())
|
|
|
|
|
}
|
|
|
|
|
if dynamicClient == nil {
|
|
|
|
|
return nil, errors.New("Error creating Dynamic client: dynamicClient is nil")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return dynamicClient, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewKubernetesService() (Infrastructure, error) {
|
|
|
|
@ -33,6 +59,7 @@ func NewKubernetesService() (Infrastructure, error) {
|
|
|
|
|
KeyData: []byte(conf.GetConfig().KubeData),
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Create clientset
|
|
|
|
|
clientset, err := kubernetes.NewForConfig(config)
|
|
|
|
|
fmt.Println("NewForConfig", clientset, err)
|
|
|
|
@ -43,6 +70,7 @@ func NewKubernetesService() (Infrastructure, error) {
|
|
|
|
|
return nil, errors.New("Error creating Kubernetes client: clientset is nil")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return &KubernetesService{
|
|
|
|
|
Set: clientset,
|
|
|
|
|
}, nil
|
|
|
|
@ -82,6 +110,9 @@ func (k *KubernetesService) CreateNamespace(ctx context.Context, ns string) erro
|
|
|
|
|
namespace := &v1.Namespace{
|
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
|
Name: ns,
|
|
|
|
|
Labels: map[string]string{
|
|
|
|
|
"multicluster-scheduler":"enabled",
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
// Create the namespace
|
|
|
|
@ -260,33 +291,29 @@ func (k *KubernetesService) CreateAdmiraltyTarget(context context.Context, execu
|
|
|
|
|
fmt.Println("Target needs to be binded to a secret in namespace ", executionId)
|
|
|
|
|
return nil, nil // Maybe we could create a wrapper for errors and add more info to have
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var targetManifest string
|
|
|
|
|
var tpl bytes.Buffer
|
|
|
|
|
tmpl, err := template.New("target").
|
|
|
|
|
Parse("{\"apiVersion\": \"multicluster.admiralty.io/v1alpha1\", \"kind\": \"Target\", \"metadata\": {\"name\": \"target-{{.ExecutionId}}\"}, \"spec\": { \"kubeconfigSecret\" :{\"name\": \"kube-secret-{{.ExecutionId}}\"}} }")
|
|
|
|
|
target := map[string]interface{}{
|
|
|
|
|
"apiVersion": "multicluster.admiralty.io/v1alpha1",
|
|
|
|
|
"kind": "Target",
|
|
|
|
|
"metadata": map[string]interface{}{
|
|
|
|
|
"name": "target-"+executionId,
|
|
|
|
|
"namespace": executionId,
|
|
|
|
|
},
|
|
|
|
|
"spec": map[string]interface{}{
|
|
|
|
|
"kubeconfigSecret": map[string]string{
|
|
|
|
|
"name" : "kube-secret-"+executionId,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
res, err := dynamicClientApply(executionId, "target", gvrTargets, context, target)
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Println("Error creating the template for the target Manifest")
|
|
|
|
|
return nil, err
|
|
|
|
|
return nil, errors.New("Error when trying to apply Source definition :" + err.Error())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = tmpl.Execute(&tpl, map[string]string{"ExecutionId": executionId})
|
|
|
|
|
targetManifest = tpl.String()
|
|
|
|
|
resp, err := postCDRapiKube(
|
|
|
|
|
*k.Set,
|
|
|
|
|
context,
|
|
|
|
|
"/apis/multicluster.admiralty.io/v1alpha1/namespaces/"+executionId+"/targets",
|
|
|
|
|
[]byte(targetManifest),
|
|
|
|
|
map[string]string{"fieldManager": "kubectl-client-side-apply"},
|
|
|
|
|
map[string]string{"fieldValidation": "Strict"},
|
|
|
|
|
)
|
|
|
|
|
return res, nil
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Println("Error trying to create a Source on remote cluster : ", err, " : ", resp)
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Admiralty Source allows a cluster to receive pods from a remote cluster
|
|
|
|
@ -297,35 +324,27 @@ func (k *KubernetesService) CreateAdmiraltyTarget(context context.Context, execu
|
|
|
|
|
// This method is temporary to implement the use of Admiralty, but must be edited
|
|
|
|
|
// to rather contact the oc-datacenter from the remote cluster to create the source
|
|
|
|
|
// locally and retrieve the token for the serviceAccount
|
|
|
|
|
func (k *KubernetesService) CreateAdmiraltySource(context context.Context, executionId string) ([]byte, error) {
|
|
|
|
|
var sourceManifest string
|
|
|
|
|
var tpl bytes.Buffer
|
|
|
|
|
tmpl, err := template.New("source").
|
|
|
|
|
Parse("{\"apiVersion\": \"multicluster.admiralty.io/v1alpha1\", \"kind\": \"Source\", \"metadata\": {\"name\": \"source-{{.ExecutionId}}\"}, \"spec\": {\"serviceAccountName\": \"sa-{{.ExecutionId}}\"} }")
|
|
|
|
|
func (k *KubernetesService) CreateAdmiraltySource(context context.Context,executionId string) ([]byte, error) {
|
|
|
|
|
|
|
|
|
|
source := map[string]interface{}{
|
|
|
|
|
"apiVersion": "multicluster.admiralty.io/v1alpha1",
|
|
|
|
|
"kind": "Source",
|
|
|
|
|
"metadata": map[string]interface{}{
|
|
|
|
|
"name": "source-"+executionId,
|
|
|
|
|
"namespace": executionId,
|
|
|
|
|
},
|
|
|
|
|
"spec": map[string]interface{}{
|
|
|
|
|
"serviceAccountName": "sa-"+executionId,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
res, err := dynamicClientApply(executionId, "source",gvrSources, context, source)
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Println("Error creating the template for the source Manifest")
|
|
|
|
|
return nil, err
|
|
|
|
|
return nil, errors.New("Error when trying to apply Source definition :" + err.Error())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = tmpl.Execute(&tpl, map[string]string{"ExecutionId": executionId})
|
|
|
|
|
sourceManifest = tpl.String()
|
|
|
|
|
|
|
|
|
|
resp, err := postCDRapiKube(
|
|
|
|
|
*k.Set,
|
|
|
|
|
context,
|
|
|
|
|
"/apis/multicluster.admiralty.io/v1alpha1/namespaces/"+executionId+"/sources",
|
|
|
|
|
[]byte(sourceManifest),
|
|
|
|
|
map[string]string{"fieldManager": "kubectl-client-side-apply"},
|
|
|
|
|
map[string]string{"fieldValidation": "Strict"},
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// We can add more info to the log with the content of resp if not nil
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Println("Error trying to create a Source on remote cluster : ", err, " : ", resp)
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
|
return res, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Create a secret from a kubeconfing. Use it to create the secret binded to an Admiralty
|
|
|
|
@ -339,33 +358,38 @@ func (k *KubernetesService) CreateKubeconfigSecret(context context.Context, kube
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
secretManifest := &v1.Secret{
|
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
|
Name: "kube-secret-" + executionId,
|
|
|
|
|
Namespace: executionId,
|
|
|
|
|
},
|
|
|
|
|
Data: map[string][]byte{
|
|
|
|
|
"config": config,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
secretApplyConfig := apply.Secret("kube-secret-" + executionId,
|
|
|
|
|
executionId).
|
|
|
|
|
WithData(map[string][]byte{
|
|
|
|
|
"config": config,
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// exists, err := k.GetKubeconfigSecret(context,executionId)
|
|
|
|
|
// if err != nil {
|
|
|
|
|
// fmt.Println("Error verifying if kube secret exists in namespace ", executionId)
|
|
|
|
|
// return nil, err
|
|
|
|
|
// }
|
|
|
|
|
// if exists != nil {
|
|
|
|
|
// fmt.Println("kube-secret already exists in namespace", executionId)
|
|
|
|
|
// fmt.Println("Overriding existing kube-secret with a newer resource")
|
|
|
|
|
// // TODO : implement DeleteKubeConfigSecret(executionID)
|
|
|
|
|
// deleted, err := k.DeleteKubeConfigSecret(executionId)
|
|
|
|
|
// _ = deleted
|
|
|
|
|
// _ = err
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
exists, err := k.GetKubeconfigSecret(context, executionId)
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Println("Error verifying if kube secret exists in namespace ", executionId)
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
if exists != nil {
|
|
|
|
|
fmt.Println("kube-secret already exists in namespace", executionId)
|
|
|
|
|
fmt.Println("Overriding existing kube-secret with a newer resource")
|
|
|
|
|
// TODO : implement DeleteKubeConfigSecret(executionID)
|
|
|
|
|
deleted, err := k.DeleteKubeConfigSecret(executionId)
|
|
|
|
|
_ = deleted
|
|
|
|
|
_ = err
|
|
|
|
|
}
|
|
|
|
|
resp, err := k.Set.CoreV1().
|
|
|
|
|
Secrets(executionId).
|
|
|
|
|
Create(context, secretManifest, metav1.CreateOptions{})
|
|
|
|
|
|
|
|
|
|
Secrets(executionId).
|
|
|
|
|
Apply(context,
|
|
|
|
|
secretApplyConfig,
|
|
|
|
|
metav1.ApplyOptions{
|
|
|
|
|
FieldManager: "admiralty-manager",
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Println("Error while trying to contact API to get secret kube-secret-" + executionId)
|
|
|
|
|
fmt.Println(err)
|
|
|
|
@ -425,7 +449,39 @@ func getCDRapiKube(client kubernetes.Clientset, ctx context.Context, path string
|
|
|
|
|
return resp, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func postCDRapiKube(client kubernetes.Clientset, ctx context.Context, path string, body []byte, params ...map[string]string) ([]byte, error) {
|
|
|
|
|
func dynamicClientApply(executionId string, typeResource string, resourceDefinition schema.GroupVersionResource, ctx context.Context, object map[string]interface{}) ([]byte, error) {
|
|
|
|
|
cli, err := NewDynamicClient()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, errors.New("Could not retrieve dynamic client when creating Admiralty Source : " + err.Error())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
res, err := cli.Resource(resourceDefinition).
|
|
|
|
|
Namespace(executionId).
|
|
|
|
|
Apply(ctx,
|
|
|
|
|
typeResource + "-" + executionId,
|
|
|
|
|
&unstructured.Unstructured{Object: object},
|
|
|
|
|
metav1.ApplyOptions{
|
|
|
|
|
FieldManager: "kubectl-client-side-apply",
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Println("Error from k8s API when applying " + fmt.Sprint(object) + " to " + gvrSources.String() + " : " , err)
|
|
|
|
|
return nil,err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// We can add more info to the log with the content of resp if not nil
|
|
|
|
|
resByte, err := json.Marshal(res)
|
|
|
|
|
if err != nil {
|
|
|
|
|
// fmt.Println("Error trying to create a Source on remote cluster : ", err , " : ", res)
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return resByte, nil
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func putCDRapiKube(client kubernetes.Clientset, ctx context.Context, path string, body []byte, params ...map[string]string) ([]byte, error){
|
|
|
|
|
req := client.RESTClient().
|
|
|
|
|
Post().
|
|
|
|
|
AbsPath(path).
|
|
|
|
|