refactored the way we apply Source and Target with dynamic client
This commit is contained in:
parent
46b7713404
commit
afe442d17f
@ -294,26 +294,6 @@ func (k *KubernetesService) CreateAdmiraltyTarget(context context.Context,execut
|
|||||||
return nil, nil // Maybe we could create a wrapper for errors and add more info to have
|
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}}\"}} }")
|
|
||||||
// if err != nil {
|
|
||||||
// fmt.Println("Error creating the template for the target Manifest")
|
|
||||||
// return nil, err
|
|
||||||
// }
|
|
||||||
|
|
||||||
// err = tmpl.Execute(&tpl, map[string]string{"ExecutionId":executionId})
|
|
||||||
// targetManifest = tpl.String()
|
|
||||||
|
|
||||||
// resp, err := putCDRapiKube(
|
|
||||||
// *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"},
|
|
||||||
// )
|
|
||||||
|
|
||||||
target := map[string]interface{}{
|
target := map[string]interface{}{
|
||||||
"apiVersion": "multicluster.admiralty.io/v1alpha1",
|
"apiVersion": "multicluster.admiralty.io/v1alpha1",
|
||||||
@ -329,38 +309,13 @@ func (k *KubernetesService) CreateAdmiraltyTarget(context context.Context,execut
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
body, err := json.Marshal(target)
|
res, err := dynamicClientApply(executionId, gvrSources, context, target)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Error creating the body from the source Manifest")
|
return nil, errors.New("Error when trying to apply Source definition :" + err.Error())
|
||||||
return nil, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
cli, err := NewDynamicClient()
|
return res, nil
|
||||||
if err != nil {
|
|
||||||
return nil, errors.New("Could not retrieve dynamic client when creating Admiralty Source : " + err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
res, err := cli.Resource(gvrTargets).
|
|
||||||
Namespace(executionId).
|
|
||||||
Apply(context,
|
|
||||||
"target-"+executionId,
|
|
||||||
&unstructured.Unstructured{Object: target},
|
|
||||||
metav1.ApplyOptions{
|
|
||||||
FieldManager: "kubectl-client-side-apply",
|
|
||||||
},
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println("Error from k8s API when applying " + string(body) + " to " + gvrSources.String() + " : " , err)
|
|
||||||
return nil,err
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -373,14 +328,6 @@ func (k *KubernetesService) CreateAdmiraltyTarget(context context.Context,execut
|
|||||||
// to rather contact the oc-datacenter from the remote cluster to create the source
|
// to rather contact the oc-datacenter from the remote cluster to create the source
|
||||||
// locally and retrieve the token for the serviceAccount
|
// locally and retrieve the token for the serviceAccount
|
||||||
func (k *KubernetesService) CreateAdmiraltySource(context context.Context,executionId string) ([]byte, error) {
|
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}}\"} }")
|
|
||||||
// if err != nil {
|
|
||||||
// fmt.Println("Error creating the template for the source Manifest")
|
|
||||||
// return nil, err
|
|
||||||
// }
|
|
||||||
|
|
||||||
source := map[string]interface{}{
|
source := map[string]interface{}{
|
||||||
"apiVersion": "multicluster.admiralty.io/v1alpha1",
|
"apiVersion": "multicluster.admiralty.io/v1alpha1",
|
||||||
@ -394,57 +341,13 @@ func (k *KubernetesService) CreateAdmiraltySource(context context.Context,execut
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
body, err := json.Marshal(source)
|
|
||||||
|
res, err := dynamicClientApply(executionId, gvrSources, context, source)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Error creating the body from the source Manifest")
|
return nil, errors.New("Error when trying to apply Source definition :" + err.Error())
|
||||||
return nil, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// err = tmpl.Execute(&tpl, map[string]string{"ExecutionId":executionId})
|
return res, nil
|
||||||
// sourceManifest = tpl.String()
|
|
||||||
// resp, err := putCDRapiKube(
|
|
||||||
// *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"},
|
|
||||||
// )
|
|
||||||
|
|
||||||
// params := []map[string]string{
|
|
||||||
// {"fieldManager":"kubectl-client-side-apply"},
|
|
||||||
// {"fieldValidation":"Strict"},
|
|
||||||
// }
|
|
||||||
|
|
||||||
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(gvrSources).
|
|
||||||
Namespace(executionId).
|
|
||||||
Apply(context,
|
|
||||||
"source-"+executionId,
|
|
||||||
&unstructured.Unstructured{Object: source},
|
|
||||||
metav1.ApplyOptions{
|
|
||||||
FieldManager: "kubectl-client-side-apply",
|
|
||||||
},
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println("Error from k8s API when applying " + string(body) + " 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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a secret from a kubeconfing. Use it to create the secret binded to an Admiralty
|
// Create a secret from a kubeconfing. Use it to create the secret binded to an Admiralty
|
||||||
@ -549,6 +452,38 @@ func getCDRapiKube(client kubernetes.Clientset, ctx context.Context, path string
|
|||||||
return resp, nil
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func dynamicClientApply(executionId 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,
|
||||||
|
"source-"+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){
|
func putCDRapiKube(client kubernetes.Clientset, ctx context.Context, path string, body []byte, params ...map[string]string) ([]byte, error){
|
||||||
req := client.RESTClient().
|
req := client.RESTClient().
|
||||||
Post().
|
Post().
|
||||||
|
Loading…
Reference in New Issue
Block a user