Skip to content

Commit

Permalink
Massive overhaul (#181)
Browse files Browse the repository at this point in the history
* massive overhaul
  • Loading branch information
knee-berts authored Oct 8, 2024
1 parent f7f20ec commit 2552f37
Show file tree
Hide file tree
Showing 66 changed files with 1,894 additions and 5,662 deletions.
55 changes: 28 additions & 27 deletions cli/cmd/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,30 @@ var applyCmd = &cobra.Command{
Use: "apply",
Short: "Create or Update GKE Demo Environment",
Example: ` gkekitctl apply
gkekitctl apply --config <file.yaml>`,
gkekitctl apply --config <file.yaml>
gkekitctl apply --config <file.yaml> -g <cluster-tfstate-clusters> -v <cluster-tfstate-network> -f <cluster-tfstate-fleet>`,
Run: func(cmd *cobra.Command, args []string) {
bucketNameNetwork, err := cmd.Flags().GetString("vpcstate")
if err != nil {
log.Errorf("🚨Failed to get bucketNameSharedVPC value from flag: %s", err)
}
if bucketNameNetwork != "" {
log.Infof("✅ Terraform state storage bucket for shared VPC is %s", bucketNameNetwork)
}
bucketNameFleet, err := cmd.Flags().GetString("fleetstate")
if err != nil {
log.Errorf("🚨Failed to get bucketNameFleet value from flag: %s", err)
}
if bucketNameFleet != "" {
log.Infof("✅ Terraform state storage bucket for Fleet is %s", bucketNameFleet)
}
bucketNameClusters, err := cmd.Flags().GetString("gkestate")
if err != nil {
log.Errorf("🚨Failed to get bucketNameClusters value from flag: %s", err)
}
if bucketNameClusters != "" {
log.Infof("✅ Terraform state storage bucket for clusters is %s", bucketNameClusters)
}
bucketNameSharedVPC, err := cmd.Flags().GetString("vpcstate")
if err != nil {
log.Errorf("🚨Failed to get bucketNameSharedVPC value from flag: %s", err)
}
if bucketNameSharedVPC != "" {
log.Infof("✅ Terraform state storage bucket for shared VPC is %s", bucketNameSharedVPC)
}

log.Info("👟 Started config validation...")
conf := config.InitConf(cfgFile)
Expand All @@ -59,46 +67,39 @@ var applyCmd = &cobra.Command{
config.GenerateTfvars(conf)

log.Info("👟 Started configuring TF State...")
err = config.CheckTfStateType(conf, bucketNameClusters, bucketNameSharedVPC)
err = config.CheckTfStateType(conf, bucketNameNetwork, bucketNameFleet, bucketNameClusters)

if err != nil {
log.Errorf("🚨 Failed setting up TF state: %s", err)
} else {
log.Info("✅ TF state configured successfully.")
}

if conf.VpcConfig.VpcType == "shared" {
lifecycle.InitTF("shared_vpc")
lifecycle.ApplyTF("shared_vpc")
}
lifecycle.InitTF("cluster_build")
lifecycle.ApplyTF("cluster_build")
lifecycle.InitTF("network")
lifecycle.ApplyTF("network")
lifecycle.InitTF("fleet")
lifecycle.ApplyTF("fleet")
lifecycle.InitTF("clusters")
lifecycle.ApplyTF("clusters")

// Authenticate Kubernetes client-go to all clusters
log.Info("☸️ Generating Kubeconfig...")
kc, err := lifecycle.GenerateKubeConfig(conf)
kc, err := lifecycle.GenerateKubeConfig(conf.FleetProjectID)
if err != nil {
log.Errorf("🚨 Failed to generate kube config: %s", err)
} else {
log.Infof("✅ Kubeconfig generated: %+v", kc)
}

// Verify access to Kubernetes API on all clusters
log.Info("☸️ Verifying Kubernetes API access for all clusters...")
err = lifecycle.ListNamespaces(kc)
if err != nil {
log.Errorf("🚨 Failed API access check on clusters: %s", err)
} else {
log.Info("✅ Clusters API access check passed.")
}
},
}

func init() {
var bucketNameClusters string
var bucketNameSharedVPC string
var bucketNameNetwork string
var bucketNameFleet string

rootCmd.AddCommand(applyCmd)
applyCmd.Flags().StringVarP(&bucketNameClusters, "gkestate", "g", "", "GKE Tf State bucket name")
applyCmd.Flags().StringVarP(&bucketNameSharedVPC, "vpcstate", "v", "", "Shared VPC Tf State bucket name")
applyCmd.Flags().StringVarP(&bucketNameNetwork, "vpcstate", "v", "", "Network Tf State bucket name")
applyCmd.Flags().StringVarP(&bucketNameFleet, "fleetstate", "f", "", "Fleet Tf State bucket name")
}
48 changes: 48 additions & 0 deletions cli/cmd/connect.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
Copyright © 2020 Google Inc.
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 cmd

import (
"gkekitctl/pkg/lifecycle"

log "github.com/sirupsen/logrus"

"github.com/spf13/cobra"
)

// connectCmd represents the connect command
var connectCmd = &cobra.Command{
Use: "connect",
Short: "connect for all clusters in GKE Demo Environment",
Example: ` gkekitctl connect -p <fleet-project-id>`,

Run: func(cmd *cobra.Command, args []string) {
log.Println("Getting credentials...")
// conf := config.InitConf(cfgFile)

log.Info("☸️ Generating Kubeconfig...")
kc, err := lifecycle.GenerateKubeConfig(fleetProjectId)
if err != nil {
log.Errorf("🚨 Failed to generate kube config: %s", err)
} else {
log.Infof("✅ Kubeconfig generated: %+v", kc)
}
},
}

func init() {
rootCmd.AddCommand(connectCmd)
}
9 changes: 3 additions & 6 deletions cli/cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ limitations under the License.
package cmd

import (
"gkekitctl/pkg/config"
"gkekitctl/pkg/lifecycle"

log "github.com/sirupsen/logrus"
Expand All @@ -32,11 +31,9 @@ var deleteCmd = &cobra.Command{

Run: func(cmd *cobra.Command, args []string) {
log.Println("Starting delete...")
conf := config.InitConf(cfgFile)
if conf.VpcConfig.VpcType == "shared" {
lifecycle.DestroyTF("shared_vpc")
}
lifecycle.DestroyTF("cluster_build")
lifecycle.DestroyTF("clusters")
lifecycle.DestroyTF("fleet")
lifecycle.DestroyTF("network")

},
}
Expand Down
2 changes: 1 addition & 1 deletion cli/cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ var initCmd = &cobra.Command{
Short: "Initialize local environment for the cli",
Example: ` gkekitctl init`,
Run: func(cmd *cobra.Command, args []string) {
folders := []string{"samples", "templates", "cluster_build", "shared_vpc"}
folders := []string{"samples", "templates", "clusters", "network", "fleet"}
err := cli_init.InitFlatFiles(folders)
if err != nil {
log.Errorf("🚨 Failed to initialize gkekitctl: %v", err)
Expand Down
16 changes: 2 additions & 14 deletions cli/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
)

var cfgFile string
var fleetProjectId string

var (
Version string
Expand All @@ -37,15 +38,6 @@ var rootCmd = &cobra.Command{
Example: ` gkekitctl create
gkectl create --config <file.yaml>
gkekitctl delete`,

// Uncomment the following line if your bare application
// has an action associated with it:
// Run: func(cmd *cobra.Command, args []string) {
// out := cmd.OutOrStdout()

// fmt.Fprintln(out, "Terraform state will be stored:", viper.GetString("terraformState"))
// fmt.Fprintln(out, conf.ConfigSync)
// },
}

// Execute adds all child commands to the root command and sets flags appropriately.
Expand All @@ -62,6 +54,7 @@ func init() {
// will be global for your application.

rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file (default is /.gkekitctl.yaml)")
rootCmd.PersistentFlags().StringVarP(&fleetProjectId, "fleet-project-id", "p", "", "Fleet project ID")

// Cobra also supports local flags, which will only run
// when this action is called directly.
Expand All @@ -77,11 +70,6 @@ func initConfig() {
// Use config file from the flag.
viper.SetConfigFile(cfgFile)
} else {
// Find home directory.
// home, err := os.UserHomeDir()
// cobra.CheckErr(err)

// Search config in home directory with name ".gkekitctl" (without extension).
viper.AddConfigPath(".")
viper.SetConfigType("yaml")
viper.SetConfigName(".gkekitctl")
Expand Down
99 changes: 92 additions & 7 deletions cli/go.mod
Original file line number Diff line number Diff line change
@@ -1,21 +1,106 @@
module gkekitctl

go 1.16
go 1.22

require (
cloud.google.com/go/compute v1.10.0
cloud.google.com/go/serviceusage v1.0.0
cloud.google.com/go/storage v1.27.0
cloud.google.com/go/compute v1.28.1
cloud.google.com/go/serviceusage v1.9.1
cloud.google.com/go/storage v1.43.0
github.com/gofrs/uuid v4.1.0+incompatible
github.com/hashicorp/go-getter v1.7.1 // indirect
github.com/hashicorp/terraform-exec v0.15.0
github.com/manifoldco/promptui v0.9.0
github.com/sirupsen/logrus v1.8.1
github.com/spf13/cobra v1.2.1
github.com/spf13/viper v1.9.0
github.com/thanhpk/randstr v1.0.4
google.golang.org/api v0.100.0
google.golang.org/genproto v0.0.0-20221025140454-527a21cfbd71
google.golang.org/api v0.196.0
google.golang.org/genproto v0.0.0-20240924160255-9d4c2d233b61
google.golang.org/genproto/googleapis/api/serviceusage v0.0.0-20240930140551-af27646dc61f
k8s.io/apimachinery v0.22.4
k8s.io/client-go v0.22.4
)

require (
cloud.google.com/go/auth v0.9.3 // indirect
cloud.google.com/go/auth/oauth2adapt v0.2.4 // indirect
cloud.google.com/go/compute/metadata v0.5.0 // indirect
cloud.google.com/go/longrunning v0.6.1 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/google/s2a-go v0.1.8 // indirect
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.54.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.54.0 // indirect
go.opentelemetry.io/otel v1.29.0 // indirect
go.opentelemetry.io/otel/metric v1.29.0 // indirect
go.opentelemetry.io/otel/trace v1.29.0 // indirect
golang.org/x/sync v0.8.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240924160255-9d4c2d233b61 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240903143218-8af14fe29dc1 // indirect
)

require (
cloud.google.com/go v0.115.1 // indirect
cloud.google.com/go/gkeconnect v0.11.1
cloud.google.com/go/iam v1.2.1 // indirect
github.com/aws/aws-sdk-go v1.44.122 // indirect
github.com/bgentry/go-netrc v0.0.0-20140422174119-9fd32a8b3d3d // indirect
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fsnotify/fsnotify v1.5.1 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/gofuzz v1.1.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.3.3 // indirect
github.com/googleapis/gax-go/v2 v2.13.0 // indirect
github.com/googleapis/gnostic v0.5.5 // indirect
github.com/hashicorp/go-checkpoint v0.5.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-getter v1.7.1 // indirect
github.com/hashicorp/go-safetemp v1.0.0 // indirect
github.com/hashicorp/go-uuid v1.0.1 // indirect
github.com/hashicorp/go-version v1.6.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/hashicorp/terraform-json v0.13.0 // indirect
github.com/imdario/mergo v0.3.12 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/json-iterator/go v1.1.11 // indirect
github.com/klauspost/compress v1.15.11 // indirect
github.com/magiconair/properties v1.8.5 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mitchellh/go-testing-interface v1.14.1 // indirect
github.com/mitchellh/mapstructure v1.4.2 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.1 // indirect
github.com/pelletier/go-toml v1.9.4 // indirect
github.com/spf13/afero v1.6.0 // indirect
github.com/spf13/cast v1.4.1 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.2.0 // indirect
github.com/ulikunitz/xz v0.5.10 // indirect
github.com/zclconf/go-cty v1.9.1 // indirect
go.opencensus.io v0.24.0 // indirect
golang.org/x/crypto v0.27.0 // indirect
golang.org/x/net v0.29.0 // indirect
golang.org/x/oauth2 v0.22.0 // indirect
golang.org/x/sys v0.25.0 // indirect
golang.org/x/term v0.24.0 // indirect
golang.org/x/text v0.18.0 // indirect
golang.org/x/time v0.6.0 // indirect
google.golang.org/grpc v1.67.0 // indirect
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/ini.v1 v1.63.2 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/api v0.22.4 // indirect
k8s.io/klog/v2 v2.130.1 // indirect
k8s.io/utils v0.0.0-20210819203725-bdf08cb9a70a // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.1.2 // indirect
sigs.k8s.io/yaml v1.2.0 // indirect
)
Loading

0 comments on commit 2552f37

Please sign in to comment.