Skip to content

Commit

Permalink
Merge pull request #2 from jjuarez/openshift-mapping
Browse files Browse the repository at this point in the history
OpenShift mapping
  • Loading branch information
jjuarez authored Feb 26, 2022
2 parents d57e18a + 3f5996e commit 2d5d8eb
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 16 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,6 @@ test: ## Testing all the things
clean: ## Clean the generated products
@$(GOCLEAN)
@rm -f $(BINARY)

.PHONY: all
all: clean build test
26 changes: 15 additions & 11 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,25 @@ func loadFile(fileName string) ([]byte, error) {
var err error
var fileContent []byte

// The parameter should be a valid filename
if fileName == "-" {
fileContent, err = ioutil.ReadAll(os.Stdin)
if err != nil {
// We have to deal with the standard input
if fileContent, err = ioutil.ReadAll(os.Stdin); err != nil {
return nil, fmt.Errorf("something went wrong reading from stdin")
}

return fileContent, nil
} else {
}

fileContent, err = ioutil.ReadFile(fileName)
if err != nil {
return nil, fmt.Errorf("something went wrong reading from file: %s", fileName)
}
if _, err := os.Stat(fileName); err != nil {
return nil, fmt.Errorf("The file: %s does not exists", fileName)
}

return fileContent, nil
if fileContent, err = ioutil.ReadFile(fileName); err != nil {
return nil, fmt.Errorf("something went wrong reading from file: %s", fileName)
}

return fileContent, nil
}

func unmarshalYAML(fileContent []byte) (*model.KubeConfig, error) {
Expand Down Expand Up @@ -81,14 +84,14 @@ func exit(err error, exitCode codes.Code) {
os.Exit(int(exitCode))
}

var InputFile string
var inputFile string

var rootCmd = &cobra.Command{
Use: "cat your_iks_kubeconfig.yaml|ikscc",
Short: "IKS context cleaner",
Long: "Small utility to clean the IBMCloud IKS kubeconfig context names",
Run: func(cmd *cobra.Command, args []string) {
fileContent, err := loadFile(InputFile)
fileContent, err := loadFile(inputFile)
if err != nil {
exit(err, codes.ReadError)
}
Expand All @@ -107,13 +110,14 @@ var rootCmd = &cobra.Command{
},
}

// Execute an entrypoint (TBC)
func Execute() {
cobra.CheckErr(rootCmd.Execute())
}

func init() {
cobra.OnInitialize(initConfig)
rootCmd.PersistentFlags().StringVarP(&InputFile, "file", "f", "", "Input file to clean")
rootCmd.PersistentFlags().StringVarP(&inputFile, "file", "f", "", "YAML k8s context file to clean")

// Streams
rootCmd.SetOut(os.Stdout)
Expand Down
15 changes: 10 additions & 5 deletions model/kubeconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ type Cluster struct {
Server string `yaml:"server,omitempty"`
}

//
// Clusters the collection of the cluster included in a context
//
type Clusters []struct {
Cluster Cluster `yaml:"cluster,omitempty"`
Name string `yaml:"name,omitempty"`
Expand Down Expand Up @@ -44,7 +47,9 @@ type UsersAuthProvider struct {
}

type User struct {
AuthProvider UsersAuthProvider `yaml:"auth-provider,omitempty"`
AuthProvider UsersAuthProvider `yaml:"auth-provider,omitempty"`
ClientCertificateDAta string `yaml:"client-certificate-data,omitempty"`
ClientKeyData string `yaml:"client-key-data,omitempty"`
}

type Users []struct {
Expand All @@ -59,11 +64,11 @@ type Preferences struct {
// KubeConfig structure
//
type KubeConfig struct {
ApiVersion string `yaml:"apiVersion,omitempty"`
Kind string `yaml:"kind,omitempty"`
ApiVersion string `yaml:"apiVersion"`
Kind string `yaml:"kind"`
Preferences Preferences `yaml:"preferences,omitempty"`
Clusters Clusters `yaml:"clusters,omitempty"`
Contexts Contexts `yaml:"contexts,omitempty"`
Clusters Clusters `yaml:"clusters"`
Contexts Contexts `yaml:"contexts"`
CurrentContext string `yaml:"current-context,omitempty"`
Users Users `yaml:"users,omitempty"`
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
19 changes: 19 additions & 0 deletions tests/fixtures/openshift_sample.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
apiVersion: v1
clusters:
- cluster:
server: https://pb5e6c0180c97826be91f-6b64a6ccc9c596bf59a86625d8fa2202-ce00.us-east.satellite.appdomain.cloud:32483
name: sat-pok-qnet-prod/c662mkmw0dj72umm0bt0
contexts:
- context:
cluster: sat-pok-qnet-prod/c662mkmw0dj72umm0bt0
namespace: default
user: admin/c662mkmw0dj72umm0bt0
name: sat-pok-qnet-prod/c662mkmw0dj72umm0bt0/admin
current-context: sat-pok-qnet-prod/c662mkmw0dj72umm0bt0/admin
kind: Config
preferences: {}
users:
- name: admin/c662mkmw0dj72umm0bt0
user:
client-certificate-data: REDACTED
client-key-data: REDACTED

0 comments on commit 2d5d8eb

Please sign in to comment.