Skip to content

Commit

Permalink
New arg for excluding resources out of support bundle
Browse files Browse the repository at this point in the history
Arg `--exclude-resources` (env `SUPPORT_BUNDLE_EXCLUDE_RESOURCES`)
accepts a comma-separated list of `<resource-name>.<group>` qualified
name of resources. Mainly serves to exclude matched resources out from
packaging into support bundle.

At this moment, only `secrets` resource is excluded by default.

Signed-off-by: Weihang Lo <weihang.lo@suse.com>
  • Loading branch information
weihanglo authored and bk201 committed Dec 20, 2021
1 parent 686af94 commit 6188b06
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
1 change: 1 addition & 0 deletions cmd/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,5 @@ func init() {
managerCmd.PersistentFlags().StringVar(&sbm.ImageName, "image-name", os.Getenv("SUPPORT_BUNDLE_IMAGE"), "The support bundle image")
managerCmd.PersistentFlags().StringVar(&sbm.ImagePullPolicy, "image-pull-policy", os.Getenv("SUPPORT_BUNDLE_IMAGE_PULL_POLICY"), "Pull policy of the support bundle image")
managerCmd.PersistentFlags().StringVar(&sbm.NodeSelector, "node-selector", os.Getenv("SUPPORT_BUNDLE_NODE_SELECTOR"), "NodeSelector of agent DaemonSet. e.g., key1=value1,key2=value2")
managerCmd.PersistentFlags().StringSliceVar(&sbm.ExcludeResourceList, "exclude-resources", getEnvStringSlice("SUPPORT_BUNDLE_EXCLUDE_RESOURCES"), "List of resources to exclude. e.g., settings.harvesterhci.io,secrets")
}
17 changes: 14 additions & 3 deletions pkg/manager/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ import (
"github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
k8sjson "k8s.io/apimachinery/pkg/runtime/serializer/json"

"github.com/rancher/support-bundle-kit/pkg/utils"
Expand Down Expand Up @@ -102,8 +104,7 @@ func (c *Cluster) generateSupportBundleYAMLs(yamlsDir string, errLog io.Writer)
type NamespacedGetter func(string) (runtime.Object, error)

func (c *Cluster) generateDiscoveredNamespacedYAMLs(namespace string, dir string, errLog io.Writer) {

objs, err := c.sbm.discovery.ResourcesForNamespace(namespace, errLog)
objs, err := c.sbm.discovery.ResourcesForNamespace(namespace, c.matchesExcludeResources, errLog)

if err != nil {
logrus.Error("Unable to fetch namespaced resources")
Expand All @@ -117,7 +118,7 @@ func (c *Cluster) generateDiscoveredNamespacedYAMLs(namespace string, dir string
}

func (c *Cluster) generateDiscoveredClusterYAMLs(dir string, errLog io.Writer) {
objs, err := c.sbm.discovery.ResourcesForCluster(errLog)
objs, err := c.sbm.discovery.ResourcesForCluster(c.matchesExcludeResources, errLog)

if err != nil {
logrus.Error("Unable to fetch cluster resources")
Expand All @@ -130,6 +131,16 @@ func (c *Cluster) generateDiscoveredClusterYAMLs(dir string, errLog io.Writer) {
}
}

// matchesExcludeResources returns true if given resource group version mathces our ExcludeResources list.
func (c *Cluster) matchesExcludeResources(gv schema.GroupVersion, resource metav1.APIResource) bool {
for _, excludeResource := range c.sbm.ExcludeResources {
if gv.Group == excludeResource.Group && resource.Name == excludeResource.Resource {
return true
}
}
return false
}

func encodeToYAMLFile(obj interface{}, path string, errLog io.Writer) {
var err error
defer func() {
Expand Down
14 changes: 14 additions & 0 deletions pkg/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/sirupsen/logrus"

v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/rest"

"github.com/rancher/support-bundle-kit/pkg/manager/client"
Expand All @@ -37,6 +38,9 @@ type SupportBundleManager struct {
PodNamespace string
NodeSelector string

ExcludeResources []schema.GroupResource
ExcludeResourceList []string

context context.Context

restConfig *rest.Config
Expand Down Expand Up @@ -140,6 +144,16 @@ func (m *SupportBundleManager) Run() error {
}

func (m *SupportBundleManager) phaseInit() error {
m.ExcludeResources = []schema.GroupResource{
// Default exclusion
{Group: v1.GroupName, Resource: "secrets"},
}
for _, res := range m.ExcludeResourceList {
gr := schema.ParseGroupResource(res)
if !gr.Empty() {
m.ExcludeResources = append(m.ExcludeResources, gr)
}
}
if err := m.check(); err != nil {
return err
}
Expand Down

0 comments on commit 6188b06

Please sign in to comment.