Skip to content

Commit

Permalink
fixed issue with manifestwork object not creating
Browse files Browse the repository at this point in the history
  • Loading branch information
clubanderson committed Apr 19, 2024
1 parent 5347820 commit 81e7c7b
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 40 deletions.
31 changes: 13 additions & 18 deletions pkg/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"k8s.io/client-go/rest"
)

var Version = "0.18.4"
var Version = "0.18.1"

// Plugin interface
type Plugin interface {
Expand Down Expand Up @@ -183,8 +183,9 @@ func (p ParamsStruct) CreateObjForPlugin(gvk schema.GroupVersionKind, yamlData [

_, err := p.createObject(p.DynamicClient, namespace, gvr, objectJSON)
if err != nil {
log.Printf(" 🔴 failed to create %v object %q in namespace %q: %v. Check if %q CRD is missing from cluster.\n", objResource, objName, namespace, err, objResource)
log.Printf(" 🔴 failed to create %v object %q in namespace %v.\n", objResource, objName, namespace)
}

}

func (p ParamsStruct) createObject(ocDynamicClientCoreOrWds dynamic.Interface, namespace string, gvr schema.GroupVersionResource, objectJSON []byte) (string, error) {
Expand All @@ -195,39 +196,30 @@ func (p ParamsStruct) createObject(ocDynamicClientCoreOrWds dynamic.Interface, n
return namespace, err
}

// Create an unstructured.Unstructured object from the map
objToCreate := &unstructured.Unstructured{Object: objMap}

// log.Printf("objToCreate: %v\n", objToCreate)
metadata, ok, _ := unstructured.NestedMap(objToCreate.Object, "Metadata")
if !ok {
fmt.Println("Metadata section not found")
return namespace, err
}
name, ok, _ := unstructured.NestedString(metadata, "Name")
if !ok {
fmt.Println("Name not found")
return namespace, err
}

// log.Printf("name: %v\n", name)
_, err = p.GetObject(ocDynamicClientCoreOrWds, namespace, gvr, name)
_, err = p.GetObject(ocDynamicClientCoreOrWds, namespace, gvr, objToCreate.GetName())
if err == nil {
// object still exists, can't create
if p.Flags["l-debug"] {
log.Printf(" ℹ️ object exists %v/%v/%v %v\n", gvr.Group, gvr.Version, gvr.Resource, name)
log.Printf(" ℹ️ object exists %v/%v/%v %v\n", gvr.Group, gvr.Version, gvr.Resource, objToCreate.GetName())
}
return namespace, err
}

// log.Printf(" ℹ️ object info %v/%v/%v %v\n", gvr.Group, gvr.Version, gvr.Resource, objToCreate.GetName())
if errors.IsNotFound(err) {
retryCount := 3
retryCount := 10
for attempt := 1; attempt <= retryCount; attempt++ {
if namespace == "" {
_, err = ocDynamicClientCoreOrWds.Resource(gvr).Create(context.TODO(), objToCreate, metav1.CreateOptions{})

} else {
_, err = ocDynamicClientCoreOrWds.Resource(gvr).Namespace(namespace).Create(context.TODO(), objToCreate, metav1.CreateOptions{})
// if err != nil {
// // log.Printf("labeler: error creating object %v/%v/%v %v in %v: %v\n", gvr.Group, gvr.Version, gvr.Resource, objToCreate.GetName(), namespace, err)
// }
}
if err == nil {
break
Expand Down Expand Up @@ -308,5 +300,8 @@ func (p ParamsStruct) GetObject(ocDynamicClientCoreOrWds dynamic.Interface, name
return objectJSON, nil
}

if err != nil {
return nil, err
}
return objectJSON, nil
}
6 changes: 4 additions & 2 deletions pkg/helpers/labeler-helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,10 @@ func AliasRun(args []string, p c.ParamsStruct) error {
}

p.ClientSet, p.RestConfig, p.DynamicClient = SwitchContext(p)

output := strings.TrimSpace(string(out))
lines := strings.Split(output, "\n")

traverseKubectlOutput(lines, p)

} else if args[0] == "helm" {
Expand Down Expand Up @@ -538,7 +540,7 @@ func getPluginNamesAndArgs(p c.ParamsStruct) {
}

if match, _ := filepath.Match("labeler-*", file.Name()); match {
log.Println("*****labeler.go: Found plugin:", file.Name())
// log.Println("*****labeler.go: Found plugin:", file.Name())
// load plugin
pi, err := plugin.Open(filepath.Join(exeDir, file.Name()))
if err != nil {
Expand Down Expand Up @@ -617,7 +619,7 @@ func SwitchContext(p c.ParamsStruct) (*kubernetes.Clientset, *rest.Config, *dyna
var kubeConfigPath string

if c.Flags.Kubeconfig == "" {
kubeConfigPath = filepath.Join(p.HomeDir, ".kube", "config")
kubeConfigPath = os.Getenv("KUBECONFIG")
} else {
kubeConfigPath = filepath.Join(c.Flags.Kubeconfig)
}
Expand Down
30 changes: 18 additions & 12 deletions pkg/plugin-bp-creator/plugin-bp-creator.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,38 +13,44 @@ import (
)

type BindingPolicy struct {
APIVersion string `yaml:"apiVersion"`
Kind string `yaml:"kind"`
Metadata Metadata `yaml:"metadata"`
Spec Spec `yaml:"spec"`
APIVersion string `json:"apiVersion"`
Kind string `json:"kind"`
Metadata Metadata `json:"metadata"`
Spec Spec `json:"spec"`
}

type Spec struct {
WantSingletonReportedState bool `yaml:"wantSingletonReportedState"`
ClusterSelectors []ClusterSelector `yaml:"clusterSelectors"`
Downsync []Downsync `yaml:"downsync"`
WantSingletonReportedState bool `json:"wantSingletonReportedState"`
ClusterSelectors []ClusterSelector `json:"clusterSelectors"`
Downsync []Downsync `json:"downsync"`
}

type Metadata struct {
Name string `yaml:"name"`
Name string `json:"name"`
}

type ClusterSelector struct {
MatchLabels map[string]string `yaml:"matchLabels"`
MatchLabels map[string]string `json:"matchLabels"`
}

type Downsync struct {
ObjectSelectors []ObjectSelector `yaml:"objectSelectors"`
ObjectSelectors []ObjectSelector `json:"objectSelectors"`
}

type ObjectSelector struct {
MatchLabels map[string]string `yaml:"matchLabels"`
MatchLabels map[string]string `json:"matchLabels"`
}

func PluginCreateBP(p c.ParamsStruct, reflect bool) []string {
// function must be exportable (capitalize first letter of function name) to be discovered by labeler
if reflect {
return []string{"l-bp-name,string,name for the bindingpolicy (usage: --l-bp-name=hello-world)", "l-bp-ns,string,namespace for the bindingpolicies (usage: --l-bp-ns=default)", "l-bp-clusterselector,string,value of clusterSelector (usage: --l-bp-clusterselector=app.kubernetes.io/part-of=sample-app)", "l-bp-wantsingletonreportedstate,flag,do you prefer singleton status for an object, if not, then grouped status will be recorded", "l-bp-wds,string,where should the object be created (usage: --l-bp-wds=namespace)"}
return []string{
"l-bp-name,string,name for the bindingpolicy (usage: --l-bp-name=hello-world)",
"l-bp-ns,string,namespace for the bindingpolicies (usage: --l-bp-ns=default)",
"l-bp-clusterselector,string,value of clusterSelector (usage: --l-bp-clusterselector=app.kubernetes.io/part-of=sample-app)",
"l-bp-wantsingletonreportedstate,flag,do you prefer singleton status for an object, if not, then grouped status will be recorded",
"l-bp-wds,string,where should the object be created (usage: --l-bp-wds=namespace)",
"l-bp-context,string,context for the object (usage: --l-bp-context=cluster)"}
}
n := "change-me"
nArg := "l-bp-name"
Expand Down
16 changes: 8 additions & 8 deletions pkg/plugin-ocm-creator/plugin-ocm-creator.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,22 @@ import (
)

type ManifestWork struct {
APIVersion string `yaml:"apiVersion"`
Kind string `yaml:"kind"`
Metadata mwMetadata `yaml:"metadata"`
Spec mwSpec `yaml:"spec"`
APIVersion string `json:"apiVersion"`
Kind string `json:"kind"`
Metadata mwMetadata `json:"metadata"`
Spec mwSpec `json:"spec"`
}

type mwMetadata struct {
Name string `yaml:"name"`
Name string `json:"name"`
}

type mwSpec struct {
Workload Workload `yaml:"workload"`
Workload Workload `json:"workload"`
}

type Workload struct {
Manifests []map[string]interface{} `yaml:"manifests"`
Manifests []map[string]interface{} `json:"manifests"`
}

// Remove the unused type declaration
Expand All @@ -38,7 +38,7 @@ type Workload struct {
func PluginCreateMW(p c.ParamsStruct, reflect bool) []string {
// function must be exportable (capitalize first letter of function name) to be discovered by labeler
if reflect {
return []string{"l-mw-name,string,name for the manifestwork object", "l-mw-create,flag,create/apply the manifestwork object"}
return []string{"l-mw-name,string,name for the manifestwork object", "l-mw-create,flag,create/apply the manifestwork object", "l-mw-namespace,string,namespace to apply the manifestwork object"}
}
// type PluginFunction struct {
// pluginCreateMW string `triggerKey:"l-mw"`
Expand Down

0 comments on commit 81e7c7b

Please sign in to comment.