Skip to content

Commit

Permalink
Script to bump example images (#3626)
Browse files Browse the repository at this point in the history
* Bump Examples Image

* comment update

* exclude hard carding

* update flag desc

---------

Co-authored-by: Mengye (Max) Gong <[email protected]>
  • Loading branch information
Kalaiselvi84 and gongmax authored Feb 7, 2024
1 parent 0596ba3 commit 903bf56
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 4 deletions.
5 changes: 5 additions & 0 deletions build/includes/website.mk
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,8 @@ update-navbar-version: FILENAME ?= ""
update-navbar-version: ensure-build-image
docker run --rm $(common_mounts) --workdir=$(mount_path) $(DOCKER_RUN_ARGS) $(build_tag) \
go run build/scripts/update-navbar-version/main.go -file=$(FILENAME)

# bump examples image
bump-image: ensure-build-image
docker run --rm $(common_mounts) --workdir=$(mount_path) $(DOCKER_RUN_ARGS) $(build_tag) \
go run build/scripts/bump-image/main.go -imageName=$(IMAGENAME) -version=$(VERSION)
106 changes: 106 additions & 0 deletions build/scripts/bump-image/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
// Copyright 2023 Google LLC All Rights Reserved.
//
// 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 main implements a program to increment the new tag for the given examples image. Run this script using `make bump-image IMAGENAME=<imageName> VERSION=<version>`
package main

import (
"flag"
"fmt"
"log"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
)

var (
imageName string
version string
versionPattern *regexp.Regexp
targetedFolders = map[string]bool{
"build": true,
"examples": true,
"install": true,
"pkg": true,
"site": true,
"test": true,
}
)

func init() {
flag.StringVar(&imageName, "imageName", "", "Image name to update")
flag.StringVar(&version, "version", "", "Version to update")
}

func main() {
flag.Parse()

if imageName == "" || version == "" {
log.Fatal("Provide both an image name and a version using the flags.")
}

versionPatternString := imageName + `:(\d+)\.(\d+)`
versionPattern = regexp.MustCompile(versionPatternString)
newVersion := incrementVersion(version)

baseDirectory := "."
for folder := range targetedFolders {
directory := filepath.Join(baseDirectory, folder)

err := filepath.Walk(directory, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() && filepath.Ext(path) != ".md" {
err = updateFileVersion(path, newVersion)
if err != nil {
log.Printf("Error updating file %s: %v", path, err)
}
}
return nil
})

if err != nil {
log.Fatalf("Error processing directory %s: %v", directory, err)
}
}
}

func incrementVersion(version string) string {
parts := strings.Split(version, ".")
if len(parts) != 2 {
log.Fatalf("Invalid version format: %s", version)
}

minor, err := strconv.Atoi(parts[1])
if err != nil {
log.Fatalf("Invalid version number: %v", err)
}

minor++
return fmt.Sprintf("%s.%d", parts[0], minor)
}

func updateFileVersion(filePath, newVersion string) error {
input, err := os.ReadFile(filePath)
if err != nil {
return err
}

content := versionPattern.ReplaceAllString(string(input), imageName+":"+newVersion)

return os.WriteFile(filePath, []byte(content), 0o644)
}
2 changes: 1 addition & 1 deletion examples/cpp-simple/fleet.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@ spec:
spec:
containers:
- name: cpp-simple
image: us-docker.pkg.dev/agones-images/examples/cpp-simple-server:0.15
image: us-docker.pkg.dev/agones-images/examples/cpp-simple-server:0.16
# imagePullPolicy: Always # add for development
2 changes: 1 addition & 1 deletion examples/cpp-simple/gameserver.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@ spec:
spec:
containers:
- name: cpp-simple
image: us-docker.pkg.dev/agones-images/examples/cpp-simple-server:0.15
image: us-docker.pkg.dev/agones-images/examples/cpp-simple-server:0.16
imagePullPolicy: Always # add for development
2 changes: 1 addition & 1 deletion install/helm/agones/templates/tests/test-runner.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ spec:
serviceAccountName: agones-controller
containers:
- name: create-gameserver
image: us-docker.pkg.dev/agones-images/examples/crd-client:0.13
image: us-docker.pkg.dev/agones-images/examples/crd-client:0.14
imagePullPolicy: Always
env:
- name: GAMESERVER_IMAGE
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func TestCppSimpleGameServerReady(t *testing.T) {
Containers: []corev1.Container{
{
Name: "cpp-simple",
Image: "us-docker.pkg.dev/agones-images/examples/cpp-simple-server:0.15",
Image: "us-docker.pkg.dev/agones-images/examples/cpp-simple-server:0.16",
},
},
},
Expand Down

0 comments on commit 903bf56

Please sign in to comment.