diff --git a/Dockerfile b/Dockerfile index a23f443..96fe8c5 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM golang:1.21.1 +FROM golang:1.23.4 COPY . / ENV CGO_ENABLED=0 @@ -6,5 +6,5 @@ ENV GOOS=linux ENV GOARCH=amd64 WORKDIR / RUN go mod download -RUN go build -ldflags="-s -w" -o ./main.go +RUN go build -ldflags="-s -w" -o ./cmd/main.go ENTRYPOINT [ "sleep","600" ] \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..125905d --- /dev/null +++ b/Makefile @@ -0,0 +1,8 @@ +test: + go test ./... -v +build: + CGO_ENABLED=0 go build -ldflags="-s -w -X main.version=dev-build" -o ./tfvar-cli ./cmd/main.go +lint-install: + @curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(GOPATH)/bin +lint: + golangci-lint run ./... -v diff --git a/cmd/main.go b/cmd/main.go new file mode 100644 index 0000000..8e50e1c --- /dev/null +++ b/cmd/main.go @@ -0,0 +1,61 @@ +package main + +import ( + "fmt" + "os" + "strings" + + "github.com/ezhische/tfvar-cli/internal/config" + "github.com/ezhische/tfvar-cli/internal/kubeclient" + "github.com/ezhische/tfvar-cli/internal/rcfile" + envparse "github.com/hashicorp/go-envparse" + coreV1 "k8s.io/api/core/v1" +) + +var ( + version string + mirrorList = []string{"terraform-mirror.yandexcloud.net", "registry.comcloud.xyz"} +) + +func main() { + cfg := config.NewConfig() + cfg.Parse() + + if *cfg.ShowVersion { + printVersion() + os.Exit(0) + } + if *cfg.Terraformrc { + if err := rcfile.CreateFile(mirrorList); err != nil { + fmt.Println(err) + os.Exit(1) + } + os.Exit(0) + } + client, err := kubeclient.New(cfg) + if err != nil { + fmt.Println(err) + os.Exit(1) + } + secret, err := client.ReadSecret(*cfg.Secret) + if err != nil { + fmt.Println(err) + os.Exit(1) + } + printSecret(cfg, secret) +} + +func printSecret(cfg *config.Config, secret *coreV1.Secret) { + for key, value := range secret.Data { + if key == *cfg.Project { + envs, _ := envparse.Parse(strings.NewReader(string(value))) + for key, value := range envs { + fmt.Printf("export %s=%s\n", key, value) + } + } + } +} + +func printVersion() { + fmt.Println("Version: ", version) +} diff --git a/go.mod b/go.mod index 6eec5ed..d83664a 100644 --- a/go.mod +++ b/go.mod @@ -1,21 +1,26 @@ -module github.com/ezhische/tfvars-cli +module github.com/ezhische/tfvar-cli -go 1.21.0 +go 1.22.4 + +require ( + github.com/hashicorp/go-envparse v0.1.0 + k8s.io/api v0.30.2 + k8s.io/apimachinery v0.30.2 + k8s.io/client-go v0.30.2 +) require ( github.com/davecgh/go-spew v1.1.1 // indirect - github.com/emicklei/go-restful/v3 v3.9.0 // indirect - github.com/go-logr/logr v1.2.4 // indirect + github.com/emicklei/go-restful/v3 v3.11.0 // indirect + github.com/go-logr/logr v1.4.1 // indirect github.com/go-openapi/jsonpointer v0.19.6 // indirect github.com/go-openapi/jsonreference v0.20.2 // indirect github.com/go-openapi/swag v0.22.3 // indirect github.com/gogo/protobuf v1.3.2 // indirect - github.com/golang/protobuf v1.5.3 // indirect + github.com/golang/protobuf v1.5.4 // indirect github.com/google/gnostic-models v0.6.8 // indirect - github.com/google/go-cmp v0.5.9 // indirect github.com/google/gofuzz v1.2.0 // indirect github.com/google/uuid v1.3.0 // indirect - github.com/hashicorp/go-envparse v0.1.0 // indirect github.com/imdario/mergo v0.3.6 // indirect github.com/josharian/intern v1.0.0 // indirect github.com/json-iterator/go v1.1.12 // indirect @@ -24,24 +29,21 @@ require ( github.com/modern-go/reflect2 v1.0.2 // indirect github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect github.com/spf13/pflag v1.0.5 // indirect - golang.org/x/net v0.17.0 // indirect - golang.org/x/oauth2 v0.8.0 // indirect - golang.org/x/sys v0.13.0 // indirect - golang.org/x/term v0.13.0 // indirect - golang.org/x/text v0.13.0 // indirect + golang.org/x/net v0.23.0 // indirect + golang.org/x/oauth2 v0.10.0 // indirect + golang.org/x/sys v0.18.0 // indirect + golang.org/x/term v0.18.0 // indirect + golang.org/x/text v0.14.0 // indirect golang.org/x/time v0.3.0 // indirect google.golang.org/appengine v1.6.7 // indirect - google.golang.org/protobuf v1.30.0 // indirect + google.golang.org/protobuf v1.33.0 // indirect gopkg.in/inf.v0 v0.9.1 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect - k8s.io/api v0.28.3 // indirect - k8s.io/apimachinery v0.28.3 // indirect - k8s.io/client-go v0.28.3 // indirect - k8s.io/klog/v2 v2.100.1 // indirect - k8s.io/kube-openapi v0.0.0-20230717233707-2695361300d9 // indirect - k8s.io/utils v0.0.0-20230406110748-d93618cff8a2 // indirect + k8s.io/klog/v2 v2.120.1 // indirect + k8s.io/kube-openapi v0.0.0-20240228011516-70dd3763d340 // indirect + k8s.io/utils v0.0.0-20230726121419-3b25d923346b // indirect sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect - sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect + sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect sigs.k8s.io/yaml v1.3.0 // indirect ) diff --git a/go.sum b/go.sum index 70bb0b2..7a72b57 100644 --- a/go.sum +++ b/go.sum @@ -2,31 +2,33 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ3 github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/emicklei/go-restful/v3 v3.9.0 h1:XwGDlfxEnQZzuopoqxwSEllNcCOM9DhhFyhFIIGKwxE= -github.com/emicklei/go-restful/v3 v3.9.0/go.mod h1:6n3XBCmQQb25CM2LCACGz8ukIrRry+4bhvbpWn3mrbc= -github.com/go-logr/logr v1.2.0/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= -github.com/go-logr/logr v1.2.4 h1:g01GSCwiDw2xSZfjJ2/T9M+S6pFdcNtFYsp+Y43HYDQ= -github.com/go-logr/logr v1.2.4/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= +github.com/emicklei/go-restful/v3 v3.11.0 h1:rAQeMHw1c7zTmncogyy8VvRZwtkmkZ4FxERmMY4rD+g= +github.com/emicklei/go-restful/v3 v3.11.0/go.mod h1:6n3XBCmQQb25CM2LCACGz8ukIrRry+4bhvbpWn3mrbc= +github.com/go-logr/logr v1.4.1 h1:pKouT5E8xu9zeFC39JXRDukb6JFQPXM5p5I91188VAQ= +github.com/go-logr/logr v1.4.1/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= github.com/go-openapi/jsonpointer v0.19.6 h1:eCs3fxoIi3Wh6vtgmLTOjdhSpiqphQ+DaPn38N2ZdrE= github.com/go-openapi/jsonpointer v0.19.6/go.mod h1:osyAmYz/mB/C3I+WsTTSgw1ONzaLJoLCyoi6/zppojs= github.com/go-openapi/jsonreference v0.20.2 h1:3sVjiK66+uXK/6oQ8xgcRKcFgQ5KXa2KvnJRumpMGbE= github.com/go-openapi/jsonreference v0.20.2/go.mod h1:Bl1zwGIM8/wsvqjsOQLJ/SH+En5Ap4rVB5KVcIDZG2k= github.com/go-openapi/swag v0.22.3 h1:yMBqmnQ0gyZvEb/+KzuWZOXgllrXT4SADYbvDaXHv/g= github.com/go-openapi/swag v0.22.3/go.mod h1:UzaqsxGiab7freDnrUUra0MwWfN/q7tE4j+VcZ0yl14= +github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 h1:tfuBGBXKqDEevZMzYi5KSi8KkcZtzBcTgAUUtapy0OI= +github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572/go.mod h1:9Pwr4B2jHnOSGXyyzV8ROjYa2ojvAY6HCGYYfMoC3Ls= github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q= github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q= github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= -github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= -github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg= -github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= +github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek= +github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps= github.com/google/gnostic-models v0.6.8 h1:yo/ABAfM5IMRsS1VnXjTBvUb61tFIHozhlYvRgGre9I= github.com/google/gnostic-models v0.6.8/go.mod h1:5n7qKqH0f5wFt+aWF8CW6pZLLNOfYuF5OpfBSENuI8U= -github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= -github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= +github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/google/gofuzz v1.2.0 h1:xRy4A+RhZaiKjJ1bPfwQ8sedCA+YS2YcCHW6ec7JMi0= github.com/google/gofuzz v1.2.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/google/pprof v0.0.0-20210720184732-4bb14d4b1be1 h1:K6RDEckDVWvDI9JAJYCmNdQXq6neHJOYx3V6jnqNEec= +github.com/google/pprof v0.0.0-20210720184732-4bb14d4b1be1/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE= github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/hashicorp/go-envparse v0.1.0 h1:bE++6bhIsNCPLvgDZkYqo3nA+/PFI51pkrHdmPSDFPY= @@ -40,8 +42,11 @@ github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHm github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8= github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= +github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= +github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0= github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc= @@ -52,7 +57,14 @@ github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9G github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA= github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ= +github.com/onsi/ginkgo/v2 v2.15.0 h1:79HwNRBAZHOEwrczrgSOPy+eFTTlIGELKy5as+ClttY= +github.com/onsi/ginkgo/v2 v2.15.0/go.mod h1:HlxMHtYF57y6Dpf+mc5529KKmSq9h2FpCF+/ZkwUxKM= +github.com/onsi/gomega v1.31.0 h1:54UJxxj6cPInHS3a35wm6BK/F9nHYueZ1NVujHDrnXE= +github.com/onsi/gomega v1.31.0/go.mod h1:DW9aCi7U6Yi40wNVAvT6kzFnEVEI5n3DloYBiKiT6zk= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ= +github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog= github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= @@ -62,6 +74,8 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= @@ -74,42 +88,43 @@ golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= -golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM= -golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE= -golang.org/x/oauth2 v0.8.0 h1:6dkIjl3j3LtZ/O3sTgZTMsLKSftL/B8Zgq4huOIIUu8= -golang.org/x/oauth2 v0.8.0/go.mod h1:yr7u4HXZRm1R1kBWqr/xKNqewf0plRYoB7sla+BCIXE= +golang.org/x/net v0.23.0 h1:7EYJ93RZ9vYSZAIb2x3lnuvqO5zneoD6IvWjuhfxjTs= +golang.org/x/net v0.23.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg= +golang.org/x/oauth2 v0.10.0 h1:zHCpF2Khkwy4mMB4bv0U37YtJdTGW8jI0glAApi0Kh8= +golang.org/x/oauth2 v0.10.0/go.mod h1:kTpgurOux7LqtuxjuyZa4Gj2gdezIt/jQtGnNFfypQI= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE= -golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/term v0.13.0 h1:bb+I9cTfFazGW51MZqBVmZy7+JEJMouUHTUSKVQLBek= -golang.org/x/term v0.13.0/go.mod h1:LTmsnFJwVN6bCy1rVCoS+qHT1HhALEFxKncY3WNNh4U= +golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4= +golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/term v0.18.0 h1:FcHjZXDMxI8mM3nwhX9HlKop4C0YQvCVCdwYl2wOtE8= +golang.org/x/term v0.18.0/go.mod h1:ILwASektA3OnRv7amZ1xhE/KTR+u50pbXfZ03+6Nx58= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k= -golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= +golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= +golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= golang.org/x/time v0.3.0 h1:rg5rLMjNzMS1RkNLzCG38eapWhnYLFYXDXj2gOlr8j4= golang.org/x/time v0.3.0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= +golang.org/x/tools v0.18.0 h1:k8NLag8AGHnn+PHbl7g43CtqZAwG60vZkLqgyZgIHgQ= +golang.org/x/tools v0.18.0/go.mod h1:GL7B4CwcLLeo59yx/9UWWuNOW1n3VZ4f5axWfML7Lcg= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/appengine v1.6.7 h1:FZR1q0exgwxzPzp/aF+VccGrSfxfPpkBqjIIEq3ru6c= google.golang.org/appengine v1.6.7/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc= -google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= -google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= -google.golang.org/protobuf v1.30.0 h1:kPPoIgf3TsEvrm0PFe15JQ+570QVxYzEvvHqChK+cng= -google.golang.org/protobuf v1.30.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= +google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI= +google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= gopkg.in/inf.v0 v0.9.1 h1:73M5CoZyi3ZLMOyDlQh031Cx6N9NDJ2Vvfl76EDAgDc= gopkg.in/inf.v0 v0.9.1/go.mod h1:cWUDdTG/fYaXco+Dcufb5Vnc6Gp2YChqWtbxRZE0mXw= @@ -119,21 +134,21 @@ gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= -k8s.io/api v0.28.3 h1:Gj1HtbSdB4P08C8rs9AR94MfSGpRhJgsS+GF9V26xMM= -k8s.io/api v0.28.3/go.mod h1:MRCV/jr1dW87/qJnZ57U5Pak65LGmQVkKTzf3AtKFHc= -k8s.io/apimachinery v0.28.3 h1:B1wYx8txOaCQG0HmYF6nbpU8dg6HvA06x5tEffvOe7A= -k8s.io/apimachinery v0.28.3/go.mod h1:uQTKmIqs+rAYaq+DFaoD2X7pcjLOqbQX2AOiO0nIpb8= -k8s.io/client-go v0.28.3 h1:2OqNb72ZuTZPKCl+4gTKvqao0AMOl9f3o2ijbAj3LI4= -k8s.io/client-go v0.28.3/go.mod h1:LTykbBp9gsA7SwqirlCXBWtK0guzfhpoW4qSm7i9dxo= -k8s.io/klog/v2 v2.100.1 h1:7WCHKK6K8fNhTqfBhISHQ97KrnJNFZMcQvKp7gP/tmg= -k8s.io/klog/v2 v2.100.1/go.mod h1:y1WjHnz7Dj687irZUWR/WLkLc5N1YHtjLdmgWjndZn0= -k8s.io/kube-openapi v0.0.0-20230717233707-2695361300d9 h1:LyMgNKD2P8Wn1iAwQU5OhxCKlKJy0sHc+PcDwFB24dQ= -k8s.io/kube-openapi v0.0.0-20230717233707-2695361300d9/go.mod h1:wZK2AVp1uHCp4VamDVgBP2COHZjqD1T68Rf0CM3YjSM= -k8s.io/utils v0.0.0-20230406110748-d93618cff8a2 h1:qY1Ad8PODbnymg2pRbkyMT/ylpTrCM8P2RJ0yroCyIk= -k8s.io/utils v0.0.0-20230406110748-d93618cff8a2/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0= +k8s.io/api v0.30.2 h1:+ZhRj+28QT4UOH+BKznu4CBgPWgkXO7XAvMcMl0qKvI= +k8s.io/api v0.30.2/go.mod h1:ULg5g9JvOev2dG0u2hig4Z7tQ2hHIuS+m8MNZ+X6EmI= +k8s.io/apimachinery v0.30.2 h1:fEMcnBj6qkzzPGSVsAZtQThU62SmQ4ZymlXRC5yFSCg= +k8s.io/apimachinery v0.30.2/go.mod h1:iexa2somDaxdnj7bha06bhb43Zpa6eWH8N8dbqVjTUc= +k8s.io/client-go v0.30.2 h1:sBIVJdojUNPDU/jObC+18tXWcTJVcwyqS9diGdWHk50= +k8s.io/client-go v0.30.2/go.mod h1:JglKSWULm9xlJLx4KCkfLLQ7XwtlbflV6uFFSHTMgVs= +k8s.io/klog/v2 v2.120.1 h1:QXU6cPEOIslTGvZaXvFWiP9VKyeet3sawzTOvdXb4Vw= +k8s.io/klog/v2 v2.120.1/go.mod h1:3Jpz1GvMt720eyJH1ckRHK1EDfpxISzJ7I9OYgaDtPE= +k8s.io/kube-openapi v0.0.0-20240228011516-70dd3763d340 h1:BZqlfIlq5YbRMFko6/PM7FjZpUb45WallggurYhKGag= +k8s.io/kube-openapi v0.0.0-20240228011516-70dd3763d340/go.mod h1:yD4MZYeKMBwQKVht279WycxKyM84kkAx2DPrTXaeb98= +k8s.io/utils v0.0.0-20230726121419-3b25d923346b h1:sgn3ZU783SCgtaSJjpcVVlRqd6GSnlTLKgpAAttJvpI= +k8s.io/utils v0.0.0-20230726121419-3b25d923346b/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0= sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd h1:EDPBXCAspyGV4jQlpZSudPeMmr1bNJefnuqLsRAsHZo= sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd/go.mod h1:B8JuhiUyNFVKdsE8h686QcCxMaH6HrOAZj4vswFpcB0= -sigs.k8s.io/structured-merge-diff/v4 v4.2.3 h1:PRbqxJClWWYMNV1dhaG4NsibJbArud9kFxnAMREiWFE= -sigs.k8s.io/structured-merge-diff/v4 v4.2.3/go.mod h1:qjx8mGObPmV2aSZepjQjbmb2ihdVs8cGKBraizNC69E= +sigs.k8s.io/structured-merge-diff/v4 v4.4.1 h1:150L+0vs/8DA78h1u02ooW1/fFq/Lwr+sGiqlzvrtq4= +sigs.k8s.io/structured-merge-diff/v4 v4.4.1/go.mod h1:N8hJocpFajUSSeSJ9bOZ77VzejKZaXsTtZo4/u7Io08= sigs.k8s.io/yaml v1.3.0 h1:a2VclLzOGrwOHDiV8EfBGhvjHvP46CtW5j6POvhYGGo= sigs.k8s.io/yaml v1.3.0/go.mod h1:GeOyir5tyXNByN85N/dRIT9es5UQNerPYEKK56eTBm8= diff --git a/internal/config/config.go b/internal/config/config.go new file mode 100644 index 0000000..e141ebe --- /dev/null +++ b/internal/config/config.go @@ -0,0 +1,33 @@ +package config + +import ( + "flag" +) + +type Config struct { + Project *string + Secret *string + Namespace *string + ClusterMode *bool + ConfigFile *string + Terraformrc *bool + ShowVersion *bool + Context *string +} + +func NewConfig() *Config { + return &Config{ + Project: flag.String("project", "none", "project name for tfvars"), + Secret: flag.String("secret", "test", "Secret name for tfvars"), + Namespace: flag.String("n", "default", "Namespace for secret"), + ClusterMode: flag.Bool("cluster", true, "Set -cluster=false for local test"), + ConfigFile: flag.String("config", "~/.kube/config", "Config file path. Default ~/.kube/config"), + Terraformrc: flag.Bool("terraformrc", false, "Choose terraformrc mirror"), + ShowVersion: flag.Bool("version", false, "Print version"), + Context: flag.String("context", "pulse/agents:dev-test", "Context for kubeconfig"), + } +} + +func (c *Config) Parse() { + flag.Parse() +} diff --git a/internal/kubeclient/kubeclient.go b/internal/kubeclient/kubeclient.go new file mode 100644 index 0000000..77f199e --- /dev/null +++ b/internal/kubeclient/kubeclient.go @@ -0,0 +1,55 @@ +package kubeclient + +import ( + "context" + "fmt" + "os" + + "github.com/ezhische/tfvar-cli/internal/config" + coreV1 "k8s.io/api/core/v1" + metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1" + k8s "k8s.io/client-go/kubernetes" + coreV1Types "k8s.io/client-go/kubernetes/typed/core/v1" + "k8s.io/client-go/rest" + cmd "k8s.io/client-go/tools/clientcmd" +) + +type Client struct { + c coreV1Types.SecretInterface +} + +func New(cfg *config.Config) (*Client, error) { + var err error + var config *rest.Config + if *cfg.ClusterMode { + config, err = buildConfigFromFlags(*cfg.Context, os.Getenv("KUBECONFIG")) + } else { + config, err = cmd.BuildConfigFromFlags("", *cfg.ConfigFile) + } + if err != nil { + return nil, fmt.Errorf("client config creation failed: %w", err) + } + clientset, err := k8s.NewForConfig(config) + if err != nil { + return nil, fmt.Errorf("client creation failed: %w", err) + } + secretsClient := clientset.CoreV1().Secrets(*cfg.Namespace) + + return &Client{c: secretsClient}, nil +} + +func (c *Client) ReadSecret(name string) (*coreV1.Secret, error) { + secret, err := c.c.Get(context.Background(), name, metaV1.GetOptions{}) + if err != nil { + return nil, fmt.Errorf("failed to get secret %s with error %w", name, err) + } + return secret, nil +} + +func buildConfigFromFlags(context, kubeconfigPath string) (*rest.Config, error) { + return cmd.NewNonInteractiveDeferredLoadingClientConfig( + &cmd.ClientConfigLoadingRules{ExplicitPath: kubeconfigPath}, + &cmd.ConfigOverrides{ + CurrentContext: context, + }).ClientConfig() +} diff --git a/internal/kubeclient/kubeclient_test.go b/internal/kubeclient/kubeclient_test.go new file mode 100644 index 0000000..001c9e0 --- /dev/null +++ b/internal/kubeclient/kubeclient_test.go @@ -0,0 +1,87 @@ +package kubeclient + +import ( + "io/fs" + "os" + "testing" + + "github.com/ezhische/tfvar-cli/internal/config" +) + +var cfg *config.Config + +func TestConfig(t *testing.T) { + cfg = config.NewConfig() +} +func TestInitClient(t *testing.T) { + *cfg.ClusterMode = true + *cfg.Context = "test" + t.Setenv("KUBECONFIG", "/dev/null") + _, got := New(cfg) + if got == nil { + t.Errorf("got %s; want nil", got) + } +} + +func TestInitClient2(t *testing.T) { + *cfg.ClusterMode = false + *cfg.ConfigFile = "/tmp/kubetestconfig" + if err := os.WriteFile("/tmp/kubetestconfig", []byte(configTemplate), fs.FileMode(0777)); err != nil { + t.Errorf("got %t; want nil", err) + } + _, got := New(cfg) + os.Remove("/tmp/kubetestconfig") + if got != nil { + t.Errorf("got %t; want nil", got) + } +} + +func TestBuildConfigFromFlags(t *testing.T) { + if err := os.WriteFile("/tmp/kubetestconfig", []byte(configTemplate), fs.FileMode(0777)); err != nil { + t.Errorf("got %t; want nil", err) + } + _, got := buildConfigFromFlags("test", "/tmp/kubetestconfig") + os.Remove("/tmp/kubetestconfig") + if got == nil { + t.Errorf("got %t; want Error", got) + } +} + +func TestBuildConfigFromFlags2(t *testing.T) { + _, got := buildConfigFromFlags("test", "/tmp/fakeconfig") + if got == nil { + t.Errorf("got %t; want Error", got) + } +} + +func TestBuildConfigFromFlags3(t *testing.T) { + if err := os.WriteFile("/tmp/kubetestconfig", []byte(configTemplate), fs.FileMode(0777)); err != nil { + t.Errorf("got %t; want nil", err) + } + _, got := buildConfigFromFlags("default", "/tmp/kubetestconfig") + os.Remove("/tmp/kubetestconfig") + if got != nil { + t.Errorf("got %t; want Error", got) + } +} + +const configTemplate = `apiVersion: v1 +clusters: +- cluster: + certificate-authority-data: + server: https://10.10.10.10:6443 + name: dr-prod-cluster +contexts: +- context: + cluster: dr-prod-cluster + user: kubernetes-admin + name: default +current-context: default +kind: Config +preferences: {} +users: +- name: kubernetes-admin + user: + client-certificate-data: + client-key-data: +` diff --git a/internal/rcfile/rcfile.go b/internal/rcfile/rcfile.go new file mode 100644 index 0000000..705e3b6 --- /dev/null +++ b/internal/rcfile/rcfile.go @@ -0,0 +1,70 @@ +package rcfile + +import ( + "bytes" + "context" + _ "embed" + "fmt" + "html/template" + "log" + "net/http" + "os" +) + +const ( + defaultMode = 0644 + urlTemplate = "https://%s/registry.terraform.io/hashicorp/random/index.json" + rcFileName = "/.terraformrc" +) + +var ( + //go:embed terraformrc.tpl + terrafromrcTemplateStr string + terrafromrcTemplate = template.Must(template.New("terrafromrc").Parse(terrafromrcTemplateStr)) +) + +func TemplFile() *template.Template { + return terrafromrcTemplate +} + +func writeFile(mirrorList []string) error { + rcfilepath := os.Getenv("HOME") + rcFileName + for _, elem := range mirrorList { + if checkMirror(elem) { + if err := os.WriteFile(rcfilepath, templateFile(elem), defaultMode); err != nil { + return err + } + break + } + } + return nil +} + +func CreateFile(mirrorList []string) error { + err := writeFile(mirrorList) + return err +} + +func checkMirror(mirr string) bool { + link := fmt.Sprintf(urlTemplate, mirr) + req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, link, nil) + if err != nil { + return false + } + resp, err := http.DefaultClient.Do(req) + if err != nil { + return false + } + defer resp.Body.Close() + return resp.StatusCode == http.StatusOK +} + +func templateFile(site string) []byte { + terraformrcBuffer := new(bytes.Buffer) + template := TemplFile() + err := template.Execute(terraformrcBuffer, site) + if err != nil { + log.Panic(err.Error()) + } + return terraformrcBuffer.Bytes() +} diff --git a/internal/rcfile/rcfile_test.go b/internal/rcfile/rcfile_test.go new file mode 100644 index 0000000..3268aac --- /dev/null +++ b/internal/rcfile/rcfile_test.go @@ -0,0 +1,57 @@ +package rcfile + +import ( + "fmt" + "testing" +) + +func TestCheckMirror(t *testing.T) { + got := checkMirror("mail.com") + want := false + if got != want { + t.Errorf("got %t; want %t", got, want) + } +} +func TestCheckMirror2(t *testing.T) { + got := checkMirror("asdfasf.com") + want := false + if got != want { + t.Errorf("got %t; want %t", got, want) + } +} +func TestTerraformrcFile(t *testing.T) { + got := string(templateFile("mail.ru")) + want := testTemplate + if got != want { + fmt.Println(len(got), len(want)) + t.Errorf("got %s; want %s", got, want) + } +} + +var testTemplate = `provider_installation { + network_mirror { + url = "https://mail.ru/" + include = ["registry.terraform.io/*/*"] + } + direct { + exclude = ["registry.terraform.io/*/*"] + } +}` + +func TestCreateTerraformrc(t *testing.T) { + t.Setenv("HOME", "/s/sda") + mirrors := []string{"terraform-mirror.yandexcloud.net", "registry.comcloud.xyz"} + got := CreateFile(mirrors) + if got == nil { + t.Errorf("got %t; want Error", got) + } +} + +func TestCreateTerraformrc2(t *testing.T) { + t.Setenv("HOME", "/tmp") + mirrors := []string{"terraform-mirror.yandexcloud.net", "registry.comcloud.xyz"} + got := CreateFile(mirrors) + if got != nil { + t.Errorf("got %t; want nil", got) + } +} diff --git a/template.txt b/internal/rcfile/terraformrc.tpl similarity index 100% rename from template.txt rename to internal/rcfile/terraformrc.tpl diff --git a/main.go b/main.go deleted file mode 100644 index bda7f13..0000000 --- a/main.go +++ /dev/null @@ -1,144 +0,0 @@ -package main - -import ( - "bytes" - "context" - _ "embed" - "flag" - "fmt" - "log" - "net/http" - "os" - "strings" - "sync" - "text/template" - - envparse "github.com/hashicorp/go-envparse" - coreV1 "k8s.io/api/core/v1" - metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1" - k8s "k8s.io/client-go/kubernetes" - coreV1Types "k8s.io/client-go/kubernetes/typed/core/v1" - "k8s.io/client-go/rest" - "k8s.io/client-go/tools/clientcmd" -) - -var ( - //go:embed template.txt - terrafromrcTemplateStr string - terrafromrcTemplate = template.Must(template.New("terrafromrc").Parse(terrafromrcTemplateStr)) - version string - mirrorList = [...]string{"terraform-mirror.yandexcloud.net", "registry.comcloud.xyz"} - secretsClient coreV1Types.SecretInterface - project *string = flag.String("project", "none", "project name for tfvars") - secret *string = flag.String("secret", "test", "Secret name for tfvars") - namespase *string = flag.String("n", "default", "Namespace for secret") - clusterMode *bool = flag.Bool("cluster", true, "Set -cluster=false for local test") - configFile *string = flag.String("config", "config", "Config file name. Defaut config") - terraformrc *bool = flag.Bool("terraformrc", false, "Chose terraformrc mirror") - showversion *bool = flag.Bool("version", false, "Print version") - kubepath string = "/.kube/" - ctx *string = flag.String("context", "pulse/agents:dev-test", "Context for kubeconfig") -) - -func printVersion() { - fmt.Println("Version: ", version) - os.Exit(0) -} - -func initClient() error { - var err error - var config *rest.Config - if *clusterMode { - kubeconfig := os.Getenv("KUBECONFIG") - config, err = buildConfigFromFlags(*ctx, kubeconfig) - } else { - kubeconfig := os.Getenv("HOME") + kubepath + *configFile - config, err = clientcmd.BuildConfigFromFlags("", kubeconfig) - } - if err != nil { - return err - } - clientset, err := k8s.NewForConfig(config) - if err != nil { - return err - } - secretsClient = clientset.CoreV1().Secrets(*namespase) - return nil -} - -func main() { - wg := new(sync.WaitGroup) - defer wg.Wait() - flag.Parse() - if *showversion { - printVersion() - } - if *terraformrc { - wg.Add(1) - createTerraformrc(wg) - } else { - if _, err := readAndPrintSecret(*secret); err != nil { - log.Panic(err.Error()) - } - } -} - -func createTerraformrc(wg *sync.WaitGroup) error { - rcfilepath := os.Getenv("HOME") + "/.terraformrc" - for _, elem := range mirrorList { - if checkMirror(elem) { - if err := os.WriteFile(rcfilepath, terraformrcFile(elem), 0644); err != nil { - return err - } - break - } - } - wg.Done() - return nil -} - -func readAndPrintSecret(name string) (*coreV1.Secret, error) { - if err := initClient(); err != nil { - return nil, err - } - secret, err := secretsClient.Get(context.TODO(), name, metaV1.GetOptions{}) - if err != nil { - return nil, err - } - for key, value := range secret.Data { - if string(key) == *project { - envs, _ := envparse.Parse(strings.NewReader(string(value))) - for key, value := range envs { - fmt.Printf("export %s=%s\n", key, value) - } - } - } - return secret, nil -} - -func buildConfigFromFlags(context, kubeconfigPath string) (*rest.Config, error) { - return clientcmd.NewNonInteractiveDeferredLoadingClientConfig( - &clientcmd.ClientConfigLoadingRules{ExplicitPath: kubeconfigPath}, - &clientcmd.ConfigOverrides{ - CurrentContext: context, - }).ClientConfig() -} - -func checkMirror(mirr string) bool { - link := fmt.Sprintf("https://%s/registry.terraform.io/hashicorp/random/index.json", mirr) - resp, err := http.Get(link) - if err != nil { - return false - } else { - return resp.StatusCode == 200 - } -} - -func terraformrcFile(site string) []byte { - terraformrcBuffer := new(bytes.Buffer) - err := terrafromrcTemplate.Execute(terraformrcBuffer, site) - if err != nil { - log.Panic(err.Error()) - } - return terraformrcBuffer.Bytes() -} diff --git a/main_test.go b/main_test.go deleted file mode 100644 index 552a09f..0000000 --- a/main_test.go +++ /dev/null @@ -1,124 +0,0 @@ -package main - -import ( - "fmt" - "io/fs" - "os" - "sync" - "testing" -) - -func TestCheckMirror(t *testing.T) { - got := checkMirror("mail.com") - want := false - if got != want { - t.Errorf("got %t; want %t", got, want) - } -} - -func TestTerraformrcFile(t *testing.T) { - got := string(terraformrcFile("mail.ru")) - want := testTemplate - if got != want { - fmt.Println(len(got), len(want)) - t.Errorf("got %s; want %s", got, want) - } -} - -var testTemplate = `provider_installation { - network_mirror { - url = "https://mail.ru/" - include = ["registry.terraform.io/*/*"] - } - direct { - exclude = ["registry.terraform.io/*/*"] - } -}` - -func TestInitClient(t *testing.T) { - *clusterMode = true - os.Setenv("KUBECONFIG", "/dev/null") - got := initClient() - if got == nil { - t.Errorf("got %s; want nil", got.Error()) - } -} - -func TestInitClient2(t *testing.T) { - kubepath = "/" - os.Setenv("HOME", "/tmp") - *configFile = "kubetestconfig" - os.WriteFile("/tmp/kubetestconfig", []byte(configTemplate), fs.FileMode(0777)) - *clusterMode = false - got := initClient() - os.Remove("/tmp/kubetestconfig") - if got != nil { - t.Errorf("got %t; want nil", got) - } -} - -func TestCreateTerraformrc(t *testing.T) { - os.Setenv("HOME", "/s/sda") - wg := new(sync.WaitGroup) - wg.Add(1) - got := createTerraformrc(wg) - if got == nil { - t.Errorf("got %t; want Error", got) - } -} - -func TestCreateTerraformrc2(t *testing.T) { - os.Setenv("HOME", "/tmp") - wg := new(sync.WaitGroup) - wg.Add(1) - got := createTerraformrc(wg) - if got != nil { - t.Errorf("got %t; want nil", got) - } -} - -func TestBuildConfigFromFlags(t *testing.T) { - os.WriteFile("/tmp/kubetestconfig", []byte(configTemplate), fs.FileMode(0777)) - _, got := buildConfigFromFlags("test", "/tmp/kubetestconfig") - os.Remove("/tmp/kubetestconfig") - if got == nil { - t.Errorf("got %t; want Error", got) - } -} - -func TestBuildConfigFromFlags2(t *testing.T) { - _, got := buildConfigFromFlags("test", "/tmp/fakeconfig") - if got == nil { - t.Errorf("got %t; want Error", got) - } -} - -func TestBuildConfigFromFlags3(t *testing.T) { - os.WriteFile("/tmp/kubetestconfig", []byte(configTemplate), fs.FileMode(0777)) - _, got := buildConfigFromFlags("default", "/tmp/kubetestconfig") - os.Remove("/tmp/kubetestconfig") - if got != nil { - t.Errorf("got %t; want Error", got) - } -} - -var configTemplate = `apiVersion: v1 -clusters: -- cluster: - certificate-authority-data: - server: https://10.10.10.10:6443 - name: dr-prod-cluster -contexts: -- context: - cluster: dr-prod-cluster - user: kubernetes-admin - name: default -current-context: default -kind: Config -preferences: {} -users: -- name: kubernetes-admin - user: - client-certificate-data: - client-key-data: -`