Skip to content

Commit 0034360

Browse files
Merge pull request #44 from wunderio/feature/azure-removal
Feature/azure removal
2 parents dd8e163 + 434d4f3 commit 0034360

File tree

7 files changed

+182
-28
lines changed

7 files changed

+182
-28
lines changed

cmd/ciCloudLogin.go

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import (
1010
"path/filepath"
1111
"strings"
1212

13+
az "github.com/wunderio/silta-cli/internal/azure"
14+
1315
"github.com/spf13/cobra"
1416
)
1517

@@ -235,8 +237,32 @@ var cloudLoginCmd = &cobra.Command{
235237
log.Fatal("Cluster name required (cluster-name)")
236238
}
237239

238-
command = fmt.Sprintf("az login --service-principal --username '%s' --tenant '%s' --password '%s';", aksSPAppID, aksTenantID, aksSPPass)
239-
command += fmt.Sprintf("az aks get-credentials --only-show-errors --resource-group '%s' --name '%s' --admin", aksResourceGroup, clusterName)
240+
token, err := az.GetAuthToken(aksTenantID, aksSPAppID, aksSPPass)
241+
if err != nil {
242+
log.Fatalf("Error: %s", err)
243+
}
244+
245+
subscriptionID, err := az.GetDefaultSubscriptionID(token)
246+
if err != nil {
247+
log.Fatalf("Error: %s", err)
248+
}
249+
250+
// Create kubeconfig folder
251+
if _, err := os.Stat(filepath.Dir(kubeConfigPath)); os.IsNotExist(err) {
252+
_ = os.Mkdir(filepath.Dir(kubeConfigPath), 0750)
253+
}
254+
255+
config, err := az.GetKubeconfig(token, subscriptionID, aksResourceGroup, clusterName)
256+
if err != nil {
257+
log.Fatalf("Error: %s", err)
258+
}
259+
260+
// Write custom kubeconfig to kube config file
261+
err = os.WriteFile(kubeConfigPath, config, 0700)
262+
if err != nil {
263+
log.Fatal("Error writing kubeconfig:", err)
264+
}
265+
240266
}
241267

242268
// Execute login commands

cmd/ciImageBuild.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ var ciImageBuildCmd = &cobra.Command{
132132

133133
imageUrl := fmt.Sprintf("%s/%s/%s-%s", imageRepoHost, imageRepoProject, namespace, imageIdentifier)
134134

135-
command := fmt.Sprintf("az acr repository show --name '%s' --image '%s/%s-%s:%s' --only-show-errors 2>&1 > /dev/null", imageRepoHost, imageRepoProject, namespace, imageIdentifier, imageTag)
135+
command := fmt.Sprintf("docker manifest inspect '%s/%s/%s-%s:%s' > /dev/null 2>&1", imageRepoHost, imageRepoProject, namespace, imageIdentifier, imageTag)
136136
err := exec.Command("bash", "-c", command).Run()
137137
if err == nil {
138138
fmt.Printf("Image %s:%s already exists, existing image will be used.", imageUrl, imageTag)

cmd/ciImageLogin.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,8 @@ Available flags and environment variables:
125125
command = fmt.Sprintf("aws ecr get-login --no-include-email | bash")
126126

127127
} else if aksSPPass != "" {
128-
// AKS & ACR Login
129-
command = fmt.Sprintf("az login --service-principal --username '%s' --tenant '%s' --password '%s';\n", aksSPAppID, aksTenantID, aksSPPass)
130-
command += fmt.Sprintf("az acr login --name '%s' --only-show-errors", imageRepoHost)
128+
// ACR Login
129+
command = fmt.Sprintf("echo %q | docker login --username %q --password-stdin %s", aksSPPass, aksSPAppID, imageRepoHost)
131130
}
132131

133132
if command != "" {

go.mod

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/wunderio/silta-cli
22

3-
go 1.17
3+
go 1.19
44

55
require (
66
github.com/Luzifer/go-openssl/v4 v4.1.0
@@ -58,7 +58,7 @@ require (
5858
github.com/google/go-cmp v0.5.6 // indirect
5959
github.com/google/gofuzz v1.2.0 // indirect
6060
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
61-
github.com/google/uuid v1.2.0 // indirect
61+
github.com/google/uuid v1.3.0 // indirect
6262
github.com/gorilla/mux v1.8.0 // indirect
6363
github.com/gosuri/uitable v0.0.4 // indirect
6464
github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7 // indirect
@@ -105,19 +105,19 @@ require (
105105
github.com/sirupsen/logrus v1.8.1 // indirect
106106
github.com/spf13/cast v1.4.1 // indirect
107107
github.com/spf13/pflag v1.0.5 // indirect
108-
github.com/stretchr/testify v1.7.2 // indirect
108+
github.com/stretchr/testify v1.8.1 // indirect
109109
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f // indirect
110110
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
111111
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
112112
github.com/xlab/treeprint v0.0.0-20181112141820-a009c3971eca // indirect
113113
go.starlark.net v0.0.0-20200306205701-8dd3e2ee1dd5 // indirect
114-
golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e // indirect
115-
golang.org/x/net v0.0.0-20220225172249-27dd8689420f // indirect
114+
golang.org/x/crypto v0.7.0 // indirect
115+
golang.org/x/net v0.9.0 // indirect
116116
golang.org/x/oauth2 v0.0.0-20211104180415-d3ed0bb246c8 // indirect
117117
golang.org/x/sync v0.0.0-20220601150217-0de741cfad7f // indirect
118-
golang.org/x/sys v0.0.0-20220412211240-33da011f77ad // indirect
119-
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect
120-
golang.org/x/text v0.3.7 // indirect
118+
golang.org/x/sys v0.7.0 // indirect
119+
golang.org/x/term v0.7.0 // indirect
120+
golang.org/x/text v0.9.0 // indirect
121121
golang.org/x/time v0.0.0-20220210224613-90d013bbcef8 // indirect
122122
google.golang.org/appengine v1.6.7 // indirect
123123
google.golang.org/genproto v0.0.0-20220107163113-42d7afdf6368 // indirect

go.sum

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -356,8 +356,8 @@ github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 h1:El6M4kTTCOh6aBiKaU
356356
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510/go.mod h1:pupxD2MaaD3pAXIBCelhxNneeOaAeabZDe5s4K6zSpQ=
357357
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
358358
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
359-
github.com/google/uuid v1.2.0 h1:qJYtXnJRWmpe7m/3XlyhrsLrEURqHRM2kxzoxXqyUDs=
360-
github.com/google/uuid v1.2.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
359+
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
360+
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
361361
github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg=
362362
github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk=
363363
github.com/googleapis/gax-go/v2 v2.1.0/go.mod h1:Q3nei7sK6ybPYH7twZdmQpAd1MKb7pfu6SK+H1/DsU0=
@@ -655,16 +655,20 @@ github.com/spf13/viper v1.8.1/go.mod h1:o0Pch8wJ9BVSWGQMbra6iw0oQ5oktSIBaujf1rJH
655655
github.com/stoewer/go-strcase v1.2.0/go.mod h1:IBiWB2sKIp3wVVQ3Y035++gc+knqhUQag1KpM8ahLw8=
656656
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
657657
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
658-
github.com/stretchr/objx v0.2.0 h1:Hbg2NidpLE8veEBkEZTL3CvlkUIVzuU9jDplZO54c48=
659658
github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
659+
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
660+
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
661+
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
660662
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
661663
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
662664
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
663665
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
664666
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
665667
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
666-
github.com/stretchr/testify v1.7.2 h1:4jaiDzPyXQvSd7D0EjG45355tLlV3VOECpq10pLC+8s=
667-
github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals=
668+
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
669+
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
670+
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
671+
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
668672
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
669673
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
670674
github.com/tmc/grpc-websocket-proxy v0.0.0-20201229170055-e5319fda7802/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
@@ -746,8 +750,8 @@ golang.org/x/crypto v0.0.0-20200820211705-5c72a883971a/go.mod h1:LzIPMQfyMNhhGPh
746750
golang.org/x/crypto v0.0.0-20201002170205-7f63de1d35b0/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
747751
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
748752
golang.org/x/crypto v0.0.0-20220214200702-86341886e292/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
749-
golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e h1:T8NU3HyQ8ClP4SEE+KbFlg6n0NhuTsN4MyznaarGsZM=
750-
golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
753+
golang.org/x/crypto v0.7.0 h1:AvwMYaRytfdeVt3u6mLaxYtErKYjxA2OXjJ1HHq6t3A=
754+
golang.org/x/crypto v0.7.0/go.mod h1:pYwdfH91IfpZVANVyUOhSIPZaFoJGxTFbZhFTx+dXZU=
751755
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
752756
golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
753757
golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8=
@@ -834,8 +838,8 @@ golang.org/x/net v0.0.0-20210825183410-e898025ed96a/go.mod h1:9nx3DQGgdP8bBQD5qx
834838
golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
835839
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
836840
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
837-
golang.org/x/net v0.0.0-20220225172249-27dd8689420f h1:oA4XRj0qtSt8Yo1Zms0CUlsT3KG69V2UGQWPBxujDmc=
838-
golang.org/x/net v0.0.0-20220225172249-27dd8689420f/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
841+
golang.org/x/net v0.9.0 h1:aWJ/m6xSmxWBx+V0XRHTlrYrPG56jKsLdTFmsSsCzOM=
842+
golang.org/x/net v0.9.0/go.mod h1:d48xBJpPfHeWQsugry2m+kC02ZBRGRgulfHnEXEuWns=
839843
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
840844
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
841845
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
@@ -943,11 +947,12 @@ golang.org/x/sys v0.0.0-20211124211545-fe61309f8881/go.mod h1:oPkhp1MJrh7nUepCBc
943947
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
944948
golang.org/x/sys v0.0.0-20220114195835-da31bd327af9/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
945949
golang.org/x/sys v0.0.0-20220209214540-3681064d5158/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
946-
golang.org/x/sys v0.0.0-20220412211240-33da011f77ad h1:ntjMns5wyP/fN65tdBD4g8J5w8n015+iIIs9rtjXkY0=
947-
golang.org/x/sys v0.0.0-20220412211240-33da011f77ad/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
950+
golang.org/x/sys v0.7.0 h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU=
951+
golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
948952
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
949-
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 h1:JGgROgKl9N8DuW20oFS5gxc+lE67/N3FcwmBPMe7ArY=
950953
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
954+
golang.org/x/term v0.7.0 h1:BEvjmm5fURWqcfbSKTdpkDXYBrUS1c0m8agp14W48vQ=
955+
golang.org/x/term v0.7.0/go.mod h1:P32HKFT3hSsZrRxla30E9HqToFYAQPCMs/zFMBUFqPY=
951956
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
952957
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
953958
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
@@ -956,8 +961,9 @@ golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
956961
golang.org/x/text v0.3.4/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
957962
golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
958963
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
959-
golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
960964
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
965+
golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE=
966+
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
961967
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
962968
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
963969
golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=

internal/azure/ciAuth.go

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
package azure
2+
3+
import (
4+
b64 "encoding/base64"
5+
"encoding/json"
6+
"errors"
7+
"io"
8+
"net/http"
9+
"strings"
10+
)
11+
12+
type Base64Kubeconfig struct {
13+
Base64Kubeconfig string `json:"value"`
14+
Name string `json:"name"`
15+
}
16+
17+
type Subscription struct {
18+
SubscriptionID string `json:"subscriptionId"`
19+
}
20+
21+
// OAuth 2 token structure
22+
type tokenResponse struct {
23+
AccessToken string `json:"access_token"`
24+
TokenType string `json:"token_type"`
25+
ExpiresIn float64 `json:"expires_in"`
26+
ExtExpiresIn float64 `json:"ext_expires_in"`
27+
}
28+
29+
// Returns the first subscription ID
30+
func GetDefaultSubscriptionID(token string) (subscriptionID string, err error) {
31+
32+
req, err := http.NewRequest(http.MethodGet, "https://management.azure.com/subscriptions?api-version=2020-01-01", nil)
33+
if err != nil {
34+
return "", err
35+
}
36+
req.Header.Set("Authorization", "Bearer "+token)
37+
38+
resp, err := http.DefaultClient.Do(req)
39+
if err != nil {
40+
return "", err
41+
}
42+
defer resp.Body.Close()
43+
44+
var subscriptions struct {
45+
Value []Subscription `json:"value"`
46+
}
47+
if err := json.NewDecoder(resp.Body).Decode(&subscriptions); err != nil {
48+
return "", err
49+
}
50+
51+
if len(subscriptions.Value) == 0 {
52+
return "", errors.New("no subscriptions found")
53+
}
54+
return subscriptions.Value[0].SubscriptionID, nil
55+
}
56+
57+
// Returns access token. Failing that, returns non-nil error
58+
// tenantId - Azure tenant ID
59+
// clientId - Client ID. Can pass Service Principal ID
60+
// clientSecret - Client secret. Cant pass Service Principal password
61+
func GetAuthToken(tenantId string, clientId string, clientSecret string) (string, error) {
62+
req, err := http.NewRequest(http.MethodPost, "https://login.microsoftonline.com/"+tenantId+"/oauth2/v2.0/token", nil)
63+
if err != nil {
64+
return "", err
65+
}
66+
67+
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
68+
69+
q := req.URL.Query()
70+
q.Add("grant_type", "client_credentials")
71+
q.Add("client_id", clientId)
72+
q.Add("client_secret", clientSecret)
73+
q.Add("scope", "https://management.azure.com/.default")
74+
75+
req.Body = io.NopCloser(strings.NewReader(q.Encode()))
76+
resp, err := http.DefaultClient.Do(req)
77+
if err != nil {
78+
return "", err
79+
}
80+
defer resp.Body.Close()
81+
82+
str, err := io.ReadAll(resp.Body)
83+
if err != nil {
84+
return "", err
85+
}
86+
87+
var token tokenResponse
88+
if err := json.Unmarshal(str, &token); err != nil {
89+
return "", err
90+
}
91+
return token.AccessToken, nil
92+
}
93+
94+
func GetKubeconfig(accessToken string, subscriptionId string, resourceGroupName string, clusterName string) ([]byte, error) {
95+
req, err := http.NewRequest(http.MethodPost, "https://management.azure.com/subscriptions/"+subscriptionId+"/resourceGroups/"+resourceGroupName+"/providers/Microsoft.ContainerService/managedClusters/"+clusterName+"/listClusterAdminCredential?api-version=2023-02-01", nil)
96+
if err != nil {
97+
return nil, err
98+
}
99+
100+
req.Header.Set("Authorization", "Bearer "+accessToken)
101+
102+
resp, err := http.DefaultClient.Do(req)
103+
if err != nil {
104+
return nil, err
105+
}
106+
defer resp.Body.Close()
107+
108+
var kubeconfigs struct {
109+
Value []Base64Kubeconfig `json:"kubeconfigs"`
110+
}
111+
if err := json.NewDecoder(resp.Body).Decode(&kubeconfigs); err != nil {
112+
return nil, err
113+
}
114+
115+
if len(kubeconfigs.Value) == 0 {
116+
return nil, errors.New("no kubeconfigs found")
117+
}
118+
119+
kconfigByte, err := b64.StdEncoding.DecodeString(kubeconfigs.Value[0].Base64Kubeconfig)
120+
if err != nil {
121+
return nil, err
122+
}
123+
return kconfigByte, nil
124+
}

internal/common/ciDockerRepoFunctions.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,4 @@ func GCPListTags(jwt string, imageName string, imageRepoHost string, imageReposi
4545
tags[i] = v.(string)
4646
}
4747
return tags
48-
4948
}

0 commit comments

Comments
 (0)