Skip to content

Commit 9cc6a8a

Browse files
authored
Merge pull request #28 from spowelljr/addCheck
Add update check
2 parents dc83a25 + 64af0de commit 9cc6a8a

File tree

4 files changed

+46
-1
lines changed

4 files changed

+46
-1
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ ARCH=$(if $(findstring amd64, $(GOARCH)),x86_64,$(GOARCH))
66
KO_VERSION=0.11.2
77

88
build: ## Build the gcp-auth-webhook binary
9-
CGO_ENABLED=0 GOOS=linux go build -o out/gcp-auth-webhook server.go
9+
CGO_ENABLED=0 GOOS=linux go build -ldflags="-X 'main.Version=$(VERSION)'" -o out/gcp-auth-webhook server.go
1010

1111
.PHONY: image
1212
image: ## Create and push multiarch manifest and images

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module github.com/GoogleContainerTools/gcp-auth-webhook
33
go 1.18
44

55
require (
6+
github.com/blang/semver/v4 v4.0.0
67
k8s.io/api v0.24.1
78
k8s.io/apimachinery v0.24.1
89
)

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ github.com/PuerkitoBio/purell v1.1.1/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbt
55
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578/go.mod h1:uGdkoq3SwY9Y+13GIhn11/XLaGBb4BfwItxLd5jeuXE=
66
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs=
77
github.com/asaskevich/govalidator v0.0.0-20190424111038-f61b66f89f4a/go.mod h1:lB+ZfQJz7igIIfQNfa7Ml4HSf2uFQQRzpGGRXenZAgY=
8+
github.com/blang/semver/v4 v4.0.0 h1:1PFHFE6yCCTv8C1TeyNNarDzntLi7wMI5i/pzqYIsAM=
9+
github.com/blang/semver/v4 v4.0.0/go.mod h1:IbckMUScFkM3pff0VJDNKRiT6TG/YpiHIM2yvyW5YoQ=
810
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
911
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
1012
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=

server.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"net/http"
2323
"os"
2424

25+
"github.com/blang/semver/v4"
2526
admissionv1 "k8s.io/api/admission/v1"
2627
corev1 "k8s.io/api/core/v1"
2728
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -36,6 +37,7 @@ var (
3637
runtimeScheme = runtime.NewScheme()
3738
codecs = serializer.NewCodecFactory(runtimeScheme)
3839
deserializer = codecs.UniversalDeserializer()
40+
Version string
3941
)
4042

4143
var projectAliases = []string{
@@ -309,9 +311,49 @@ func needsEnvVar(c corev1.Container, name string) bool {
309311
return true
310312
}
311313

314+
func updateCheck() error {
315+
type release struct {
316+
Name string `json:"name"`
317+
}
318+
319+
var releases []release
320+
321+
resp, err := http.Get("https://storage.googleapis.com/minikube-gcp-auth/releases.json")
322+
if err != nil {
323+
return fmt.Errorf("failed to get releases file: %v", err)
324+
}
325+
defer resp.Body.Close()
326+
if err := json.NewDecoder(resp.Body).Decode(&releases); err != nil {
327+
return fmt.Errorf("failed to decode releases file: %v", err)
328+
}
329+
if len(releases) == 0 {
330+
return fmt.Errorf("no releases found in releases file")
331+
}
332+
333+
currVersion, err := semver.ParseTolerant(Version)
334+
if err != nil {
335+
return fmt.Errorf("unable to parse current version: %v", err)
336+
}
337+
name := releases[0].Name
338+
latestVersion, err := semver.ParseTolerant(name)
339+
if err != nil {
340+
return fmt.Errorf("unable to parse latest version: %v", err)
341+
}
342+
343+
if currVersion.LT(latestVersion) {
344+
log.Printf("gcp-auth-webhook %s is available!", name)
345+
}
346+
347+
return nil
348+
}
349+
312350
func main() {
313351
log.Print("GCP Auth Webhook started!")
314352

353+
if err := updateCheck(); err != nil {
354+
log.Println(err)
355+
}
356+
315357
mux := http.NewServeMux()
316358

317359
mux.HandleFunc("/mutate", mutateHandler)

0 commit comments

Comments
 (0)