Skip to content

Commit

Permalink
Fix SBK to eliminate empty YAML file
Browse files Browse the repository at this point in the history
Signed-off-by: Jian Wang <w13915984028@gmail.com>
  • Loading branch information
w13915984028 authored and Vicente-Cheng committed Sep 27, 2023
1 parent 5dd578f commit 70a903f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
20 changes: 16 additions & 4 deletions pkg/manager/client/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,12 @@ func (dc *DiscoveryClient) SpecificResourcesForNamespace(toObj ParseResult, modu
if err != nil {
return nil, err
}
objs[gv.String()+"/"+resource.Name] = obj
// skip empty object, which will cause useless zero item yaml file
if obj != nil {
objs[gv.String()+"/"+resource.Name] = obj
} else {
logrus.Debugf("No %s/%s resource %s in namespace %s, skip", gv.String(), resource.Kind, resource.Name, namespace)
}
}

}
Expand Down Expand Up @@ -164,13 +169,17 @@ func (dc *DiscoveryClient) ResourcesForNamespace(toObj ParseResult, namespace st
if err != nil {
return nil, err
}
objs[gv.String()+"/"+resource.Name] = obj
// skip empty object, which will cause useless zero item yaml file
if obj != nil {
objs[gv.String()+"/"+resource.Name] = obj
} else {
logrus.Debugf("No %s/%s resource %s in namespace %s, skip", gv.String(), resource.Kind, resource.Name, namespace)
}
}
}
}

return objs, nil

}

// Get the cluster level resources
Expand Down Expand Up @@ -222,7 +231,10 @@ func (dc *DiscoveryClient) ResourcesForCluster(toObj ParseResult, exclude Exclud
if err != nil {
return nil, err
}
objs[gv.String()+"/"+resource.Name] = obj
// skip empty object
if obj != nil {
objs[gv.String()+"/"+resource.Name] = obj
}
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions pkg/manager/collectors/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ func (c common) toObjCommon(b []byte, groupVersion, kind string) (*gabs.Containe
return nil, err
}

// When there is no instance of the given GVK, return nil
if len(jsonParsed.S("items").Children()) == 0 {
return nil, nil
}

for _, child := range jsonParsed.S("items").Children() {
if _, err = child.SetP(groupVersion, "apiVersion"); err != nil {
logrus.Error("Unable to set apiVersion field.")
Expand Down
2 changes: 1 addition & 1 deletion pkg/manager/collectors/harvester.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func (module harvesterModule) toClusterObj(b []byte, groupVersion, kind string,
switch kind {
case "Setting":
currentItems, _ := jsonParsed.S("items").Data().([]interface{})
logrus.Debugf("Whole items: %v", currentItems)
logrus.Debugf("Whole items in cluster: %v", currentItems)
var newItems []interface{}
for _, item := range currentItems {
gItem := gabs.Wrap(item)
Expand Down

0 comments on commit 70a903f

Please sign in to comment.