Skip to content

Commit

Permalink
Create a kompanion command to migrate KCC.
Browse files Browse the repository at this point in the history
New mgirator command.
Creates a migration file of KCC resources.
Allows the resources to be easily copied to another cluster.
  • Loading branch information
cheftako committed Nov 26, 2024
1 parent 35c7c5e commit 751af1f
Show file tree
Hide file tree
Showing 8 changed files with 682 additions and 56 deletions.
32 changes: 32 additions & 0 deletions experiments/kompanion/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Copyright 2024 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

## ---
## Basic build
## ---
.PHONY: build
build: bin
GOWORK=off go build -o bin/kompanion

bin:
mkdir bin

## ---
## Clean build and related artifacts to make the repo 'clean'
## ---
.PHONY: clean
clean:
rm -rf bin
rm migration.yaml

5 changes: 3 additions & 2 deletions experiments/kompanion/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ Experimental KCC companion tool to help troubleshoot, analyze and gather data ab

```
# Assumes pwd is <REPO_ROOT>/experiments/kompanion
$ GOWORK=off go build -o kompanion
$ mkdir bin
$ GOWORK=off go build -o bin/kompanion
```

## Export
Expand All @@ -33,4 +34,4 @@ The command will generate a timestamped report `tar.gz` file to use as a snapsho

# Light Roadmap

* [ ] Debug/ audit logs for the tool itself
* [ ] Debug/ audit logs for the tool itself
54 changes: 5 additions & 49 deletions experiments/kompanion/cmd/export/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,59 +59,23 @@ const (
)

func BuildExportCmd() *cobra.Command {
var opts ExportOptions

opts := NewExportOptions()
cmd := &cobra.Command{
Use: "export",
Short: "export Config Connector resources",
Example: examples,
RunE: func(cmd *cobra.Command, args []string) error {
return RunExport(cmd.Context(), &opts)
return RunExport(cmd.Context(), opts)
},
Args: cobra.ExactArgs(0),
}

cmd.Flags().StringVarP(&opts.kubeconfig, kubeconfigFlag, "", opts.kubeconfig, "path to the kubeconfig file.")
cmd.Flags().StringVarP(&opts.reportNamePrefix, reportNamePrefixFlag, "", "report", "Prefix for the report name. The tool appends a timestamp to this in the format \"YYYYMMDD-HHMMSS.milliseconds\".")

cmd.Flags().StringArrayVarP(&opts.targetNamespaces, targetNamespacesFlag, "", []string{}, "namespace prefix to target the export tool. Targets all if empty. Can be specified multiple times.")
cmd.Flags().StringArrayVarP(&opts.ignoreNamespaces, ignoreNamespacesFlag, "", []string{"kube"}, "namespace prefix to ignore. Excludes nothing if empty. Can be specified multiple times. Defaults to \"kube\".")

cmd.Flags().StringArrayVarP(&opts.targetObjects, targetObjectsFlag, "", []string{}, "object name prefix to target. Targets all if empty. Can be specified multiple times.")
cmd.Flags().StringArrayVarP(&opts.ignoreObjects, ignoreObjectsFlag, "", []string{}, "object name prefix to ignore. Excludes nothing if empty. Can be specified multiple times.")

cmd.Flags().IntVarP(&opts.workerRountines, workerRoutinesFlag, "", 10, "Configure the number of worker routines to export namespaces with. Defaults to 10. ")
flags := cmd.Flags()
opts.AddFlags(flags)

return cmd
}

const (
// flag names.
kubeconfigFlag = "kubeconfig"
reportNamePrefixFlag = "report-prefix"

targetNamespacesFlag = "target-namespaces"
ignoreNamespacesFlag = "exclude-namespaces"

targetObjectsFlag = "target-objects"
ignoreObjectsFlag = "exclude-objects"

workerRoutinesFlag = "worker-routines"
)

type ExportOptions struct {
kubeconfig string
reportNamePrefix string

targetNamespaces []string
ignoreNamespaces []string

targetObjects []string
ignoreObjects []string

workerRountines int
}

// Task is implemented by our namespace-collection routine, or anything else we want to run in parallel.
type Task interface {
Run(ctx context.Context) error
Expand Down Expand Up @@ -217,14 +181,6 @@ func (t *dumpResourcesTask) Run(ctx context.Context) error {
return nil
}

func (opts *ExportOptions) validateFlags() error {
if opts.workerRountines <= 0 || opts.workerRountines > 100 {
return fmt.Errorf("invalid value %d for flag %s. Supported values are [1,100]", opts.workerRountines, workerRoutinesFlag)
}

return nil
}

func getRESTConfig(ctx context.Context, opts *ExportOptions) (*rest.Config, error) {
var loadingRules clientcmd.ClientConfigLoader
if opts.kubeconfig != "" {
Expand Down Expand Up @@ -335,7 +291,7 @@ func RunExport(ctx context.Context, opts *ExportOptions) error {
var errs []error
var errsMutex sync.Mutex

for i := 0; i < opts.workerRountines; i++ {
for i := 0; i < opts.workerRoutines; i++ {
wg.Add(1)
go func() {
defer wg.Done()
Expand Down
93 changes: 93 additions & 0 deletions experiments/kompanion/cmd/export/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package export

import (
"fmt"
"log"

"github.com/spf13/pflag"
)

const (
// flag names.
kubeconfigFlag = "kubeconfig"
reportNamePrefixFlag = "report-prefix"

targetNamespacesFlag = "target-namespaces"
ignoreNamespacesFlag = "exclude-namespaces"

targetObjectsFlag = "target-objects"
ignoreObjectsFlag = "exclude-objects"

workerRoutinesFlag = "worker-routines"
)

type ExportOptions struct {
kubeconfig string
reportNamePrefix string

targetNamespaces []string
ignoreNamespaces []string

targetObjects []string
ignoreObjects []string

workerRoutines int
}

func (o *ExportOptions) AddFlags(flags *pflag.FlagSet) {
flags.StringVarP(&o.kubeconfig, kubeconfigFlag, "", o.kubeconfig, "path to the kubeconfig file.")
flags.StringVarP(&o.reportNamePrefix, reportNamePrefixFlag, "", o.reportNamePrefix, "Prefix for the report name. The tool appends a timestamp to this in the format \"YYYYMMDD-HHMMSS.milliseconds\".")

flags.StringArrayVarP(&o.targetNamespaces, targetNamespacesFlag, "", o.targetNamespaces, "namespace prefix to target the export tool. Targets all if empty. Can be specified multiple times.")
flags.StringArrayVarP(&o.ignoreNamespaces, ignoreNamespacesFlag, "", o.ignoreNamespaces, "namespace prefix to ignore. Excludes nothing if empty. Can be specified multiple times. Defaults to \"kube\".")

flags.StringArrayVarP(&o.targetObjects, targetObjectsFlag, "", o.targetObjects, "object name prefix to target. Targets all if empty. Can be specified multiple times.")
flags.StringArrayVarP(&o.ignoreObjects, ignoreObjectsFlag, "", o.ignoreObjects, "object name prefix to ignore. Excludes nothing if empty. Can be specified multiple times.")

flags.IntVarP(&o.workerRoutines, workerRoutinesFlag, "", o.workerRoutines, "Configure the number of worker routines to export namespaces with. Defaults to 10. ")
}

func (opts *ExportOptions) validateFlags() error {
if opts.workerRoutines <= 0 || opts.workerRoutines > 100 {
return fmt.Errorf("invalid value %d for flag %s. Supported values are [1,100]", opts.workerRoutines, workerRoutinesFlag)
}

return nil
}

func (o *ExportOptions) Print() {
log.Printf("kubeconfig set to %q.\n", o.kubeconfig)
log.Printf("reportNamePrefix set to %q.\n", o.reportNamePrefix)
log.Printf("targetNamespaces set to %v.\n", o.targetNamespaces)
log.Printf("ignoreNamespaces set to %v.\n", o.ignoreNamespaces)
log.Printf("targetObjects set to %v.\n", o.targetObjects)
log.Printf("ignoreObjects set to %v.\n", o.ignoreObjects)
log.Printf("workerRoutines set to %d.\n", o.workerRoutines)
}

func NewExportOptions() *ExportOptions {
o := ExportOptions{
kubeconfig: "",
reportNamePrefix: "report",
targetNamespaces: []string{},
ignoreNamespaces: []string{"kube"},
targetObjects: []string{},
ignoreObjects: []string{},
workerRoutines: 10,
}
return &o
}
Loading

0 comments on commit 751af1f

Please sign in to comment.