Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

정책 관리 K8s 연동 에러로그 추가 #403

Merged
merged 1 commit into from
Apr 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions internal/policy-template/tkspolicytemplate.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"

"github.com/openinfradev/tks-api/pkg/kubernetes"
"github.com/openinfradev/tks-api/pkg/log"
"gopkg.in/yaml.v3"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"

Expand Down Expand Up @@ -158,12 +159,16 @@ func ApplyTksPolicyTemplateCR(ctx context.Context, primaryClusterId string, tksP
dynamicClient, err := kubernetes.GetDynamicClientAdminCluster(ctx)

if err != nil {
log.Errorf(ctx, "error is :%s(%T), primaryClusterId='%s', policyTemplateName='%+v'",
err.Error(), err, primaryClusterId, tksPolicyTemplate.Name)
return err
}

policyTemplate, err := GetTksPolicyTemplateCR(ctx, primaryClusterId, strings.ToLower(tksPolicyTemplate.Kind))

if err != nil {
log.Errorf(ctx, "error is :%s(%T), primaryClusterId='%s', policyTemplateName='%+v'",
err.Error(), err, primaryClusterId, tksPolicyTemplate.Name)
if errors.IsNotFound(err) {
tksPolicyTemplateUnstructured, err := tksPolicyTemplate.ToUnstructured()

Expand All @@ -183,12 +188,19 @@ func ApplyTksPolicyTemplateCR(ctx context.Context, primaryClusterId string, tksP
tksPolicyTemplateUnstructured, err := policyTemplate.ToUnstructured()

if err != nil {
log.Errorf(ctx, "error is :%s(%T), primaryClusterId='%s', policyTemplateName='%+v'",
err.Error(), err, primaryClusterId, tksPolicyTemplate.Name)
return err
}

_, err = dynamicClient.Resource(TKSPolicyTemplateGVR).Namespace(primaryClusterId).
Update(ctx, tksPolicyTemplateUnstructured, metav1.UpdateOptions{})

if err != nil {
log.Errorf(ctx, "error is :%s(%T), primaryClusterId='%s', policyTemplateName='%+v'",
err.Error(), err, primaryClusterId, tksPolicyTemplate.Name)
}

return err
}
return nil
Expand All @@ -198,6 +210,8 @@ func DeleteTksPolicyTemplateCR(ctx context.Context, primaryClusterId string, nam
dynamicClient, err := kubernetes.GetDynamicClientAdminCluster(ctx)

if err != nil {
log.Errorf(ctx, "error is :%s(%T), primaryClusterId='%s', policyTemplateName='%+v'",
err.Error(), err, primaryClusterId, name)
return err
}

Expand All @@ -216,13 +230,18 @@ func ExistsTksPolicyTemplateCR(ctx context.Context, primaryClusterId string, nam
dynamicClient, err := kubernetes.GetDynamicClientAdminCluster(ctx)

if err != nil {
log.Errorf(ctx, "error is :%s(%T), primaryClusterId='%s', policyTemplateName='%+v'",
err.Error(), err, primaryClusterId, name)
return false, err
}

result, err := dynamicClient.Resource(TKSPolicyTemplateGVR).Namespace(primaryClusterId).
Get(ctx, name, metav1.GetOptions{})

if err != nil {
log.Errorf(ctx, "error is :%s(%T), primaryClusterId='%s', policyTemplateName='%+v'",
err.Error(), err, primaryClusterId, name)

if errors.IsNotFound(err) {
return false, nil
} else {
Expand All @@ -237,27 +256,35 @@ func GetTksPolicyTemplateCR(ctx context.Context, primaryClusterId string, name s
dynamicClient, err := kubernetes.GetDynamicClientAdminCluster(ctx)

if err != nil {
log.Errorf(ctx, "error is :%s(%T), primaryClusterId='%s', policyTemplateName='%+v'",
err.Error(), err, primaryClusterId, name)
return nil, err
}

result, err := dynamicClient.Resource(TKSPolicyTemplateGVR).Namespace(primaryClusterId).
Get(ctx, name, metav1.GetOptions{})

if err != nil {
log.Errorf(ctx, "error is :%s(%T), primaryClusterId='%s', policyTemplateName='%+v'",
err.Error(), err, primaryClusterId, name)
return nil, err
}

// Unstructured를 바로 TKSPolicyTemplate으로 컨버팅할 수 없기 때문에 json으로 변환
jsonBytes, err := json.Marshal(result.Object)

if err != nil {
log.Errorf(ctx, "error is :%s(%T), primaryClusterId='%s', policyTemplateName='%+v'",
err.Error(), err, primaryClusterId, name)
return nil, err
}

var tksPolicyTemplate TKSPolicyTemplate
err = json.Unmarshal(jsonBytes, &tksPolicyTemplate)

if err != nil {
log.Errorf(ctx, "error is :%s(%T), primaryClusterId='%s', policyTemplateName='%+v'",
err.Error(), err, primaryClusterId, name)
return nil, err
}

Expand All @@ -268,12 +295,17 @@ func UpdateTksPolicyTemplateCR(ctx context.Context, primaryClusterId string, tks
dynamicClient, err := kubernetes.GetDynamicClientAdminCluster(ctx)

if err != nil {
log.Errorf(ctx, "error is :%s(%T), primaryClusterId='%s', policyTemplateName='%+v'",
err.Error(), err, primaryClusterId, tksPolicyTemplate.Name)

return err
}

obj, err := runtime.DefaultUnstructuredConverter.ToUnstructured(tksPolicyTemplate)

if err != nil {
log.Errorf(ctx, "error is :%s(%T), primaryClusterId='%s', policyTemplateName='%+v'",
err.Error(), err, primaryClusterId, tksPolicyTemplate.Name)
return err
}

Expand All @@ -284,6 +316,12 @@ func UpdateTksPolicyTemplateCR(ctx context.Context, primaryClusterId string, tks
_, err = dynamicClient.Resource(TKSPolicyTemplateGVR).Namespace(primaryClusterId).
Update(ctx, tksPolicyUnstructured, metav1.UpdateOptions{})

if err != nil {
log.Errorf(ctx, "error is :%s(%T), primaryClusterId='%s', policyTemplateName='%+v'",
err.Error(), err, primaryClusterId, tksPolicyTemplate.Name)
return err
}

return err
}

Expand Down Expand Up @@ -331,18 +369,27 @@ func UpdateTksPolicyTemplateCR(ctx context.Context, primaryClusterId string, tks
func GetTksPolicyTemplateCRs(ctx context.Context, primaryClusterId string) (tksPolicyTemplates []TKSPolicyTemplate, err error) {
dynamicClient, err := kubernetes.GetDynamicClientAdminCluster(ctx)
if err != nil {
log.Errorf(ctx, "error is :%s(%T), primaryClusterId='%s'",
err.Error(), err, primaryClusterId)

return nil, err
}

resources, err := dynamicClient.Resource(TKSPolicyTemplateGVR).Namespace(primaryClusterId).
List(context.TODO(), metav1.ListOptions{})
if err != nil {
log.Errorf(ctx, "error is :%s(%T), primaryClusterId='%s'",
err.Error(), err, primaryClusterId)

return nil, err
}

var tksPolicyTemplate TKSPolicyTemplate
for _, c := range resources.Items {
if err = runtime.DefaultUnstructuredConverter.FromUnstructured(c.UnstructuredContent(), &tksPolicyTemplate); err != nil {
log.Errorf(ctx, "error is :%s(%T), primaryClusterId='%s', policyTemplateName='%+v'",
err.Error(), err, primaryClusterId, tksPolicyTemplate.Name)

return nil, err
}
tksPolicyTemplates = append(tksPolicyTemplates, tksPolicyTemplate)
Expand Down
Loading