Skip to content

Commit cbc68bc

Browse files
committed
docker build
Signed-off-by: James Strong <[email protected]>
1 parent 7a32b1a commit cbc68bc

File tree

6 files changed

+197
-36
lines changed

6 files changed

+197
-36
lines changed

ingressctl/cmd/common.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"strings"
7+
"time"
8+
)
9+
10+
var DEBUG bool
11+
12+
func init() {
13+
DEBUG = false
14+
debugENV := os.Getenv("MAGE_DEBUG")
15+
if debugENV == "true" {
16+
DEBUG = true
17+
}
18+
}
19+
20+
// CheckArgs should be used to ensure the right command line arguments are
21+
// passed before executing an example.
22+
func CheckArgs(arg ...string) {
23+
if len(os.Args) < len(arg)+1 {
24+
ErrorF("Usage: %s %s", os.Args[0], strings.Join(arg, " "))
25+
os.Exit(1)
26+
}
27+
}
28+
29+
// CheckIfError should be used to naively panics if an error is not nil.
30+
func CheckIfError(err error, format string, args ...interface{}) {
31+
if err == nil {
32+
return
33+
}
34+
35+
fmt.Printf("\x1b[31;1m%s ERROR %s %s\x1b[0m\n", timeStamp(), fmt.Sprintf(format, args...), err)
36+
os.Exit(1)
37+
}
38+
39+
// Info should be used to describe the example commands that are about to run.
40+
func Info(format string, args ...interface{}) {
41+
fmt.Printf("\x1b[34;1m%s INFO: %s\x1b[0m\n", timeStamp(), fmt.Sprintf(format, args...))
42+
}
43+
44+
func timeStamp() string {
45+
t := time.Now()
46+
return t.Format(time.RFC3339)
47+
}
48+
49+
// Warning should be used to display a warning
50+
func Warning(format string, args ...interface{}) {
51+
fmt.Printf("\x1b[36;1m%s WARNING: %s\x1b[0m\n", timeStamp(), fmt.Sprintf(format, args...))
52+
}
53+
54+
// Info should be used to describe the example commands that are about to run.
55+
func Debug(format string, args ...interface{}) {
56+
if DEBUG {
57+
fmt.Printf("\x1b[34;1m%s DEBUG: %s\x1b[0m\n", timeStamp(), fmt.Sprintf(format, args...))
58+
}
59+
}
60+
61+
// Info should be used to describe the example commands that are about to run.
62+
func ErrorF(format string, args ...interface{}) {
63+
fmt.Printf("\x1b[31;1m%s ERROR: %s\x1b[0m\n", timeStamp(), fmt.Sprintf(format, args...))
64+
}

ingressctl/cmd/docker.go

Lines changed: 110 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ package cmd
33
import (
44
"context"
55
"fmt"
6+
"os"
7+
"strings"
68

9+
"github.com/codeskyblue/go-sh"
710
"github.com/docker/docker/api/types"
811
"github.com/docker/docker/client"
912
"github.com/spf13/cobra"
@@ -26,18 +29,33 @@ var dockerListCmd = &cobra.Command{
2629

2730
var dockerBuildCmd = &cobra.Command{
2831
Use: "build",
29-
Short: "build a docker container"
30-
Long: "build a docker container for use with ingress-nginx"
31-
Run: func(cmd *cobra.Command, args []string){
32+
33+
Long: "build a docker container for use with ingress-nginx",
34+
Run: func(cmd *cobra.Command, args []string) {
3235
dockerBuild()
3336
},
34-
3537
}
38+
39+
var dco dockerBuildOpts
40+
3641
func init() {
3742
rootCmd.AddCommand(dockerCmd)
3843
dockerCmd.AddCommand(dockerListCmd)
44+
dockerCmd.AddCommand(dockerBuildCmd)
45+
dockerBuildCmd.Flags().StringVar(&dco.PlatformFlag, "platformflag", "", "Setting the Docker --platform build flag")
46+
dockerBuildCmd.Flags().StringSliceVar(&dco.Platform, "platforms", PLATFORMS, "comma seperated list of platforms to build for container image")
47+
dockerBuildCmd.Flags().StringVar(&dco.BuildArgs.BaseImage, "base", "", "base image to build container off of")
48+
dockerBuildCmd.Flags().StringVar(&dco.Path, "path", "", "container build path")
49+
dockerBuildCmd.Flags().StringVar(&dco.BuildArgs.Version, "version", "", "docker tag to build")
50+
dockerBuildCmd.Flags().StringVar(&dco.BuildArgs.TargetArch, "targetarch", "", "target arch to build")
51+
dockerBuildCmd.Flags().StringVar(&dco.BuildArgs.CommitSHA, "commitsha", "", "build arg commit sha to add to build")
52+
dockerBuildCmd.Flags().StringVar(&dco.BuildArgs.BuildId, "build-id", "", "build id to add to container metadata")
53+
dockerBuildCmd.Flags().StringVar(&dco.DockerFile, "dockerfile", "", "dockerfile of image to build")
54+
dockerBuildCmd.Flags().StringVar(&dco.Image.Name, "name", "", "container image name registry/name:tag@digest")
55+
dockerBuildCmd.Flags().StringVar(&dco.Image.Registry, "registry", Registry, "Registry to tag image and push container registry/name:tag@digest")
56+
dockerBuildCmd.Flags().StringVar(&dco.Image.Tag, "tag", "", "container tag registry/name:tag@digest")
57+
dockerBuildCmd.Flags().StringVar(&dco.Image.Digest, "digest", "", "digest of container image registry/name:tag@digest")
3958
}
40-
4159
func dockerList() {
4260
cli, err := client.NewClientWithOpts(client.FromEnv)
4361
if err != nil {
@@ -54,35 +72,91 @@ func dockerList() {
5472
}
5573
}
5674

75+
var PLATFORMS = []string{"amd64", "arm", "arm64", "s390x"}
76+
var BUILDX_PLATFORMS = "linux/amd64,linux/arm,linux/arm64,linux/s390x"
77+
var Registry = "gcr.io/k8s-staging-ingress-nginx"
78+
var PKG = "k8s.io/ingress-nginx"
79+
80+
type dockerBuildOpts struct {
81+
PlatformFlag string
82+
Platform []string
83+
BuildArgs BuildArgs
84+
DockerFile string
85+
Image Image
86+
Path string
87+
}
88+
89+
type BuildArgs struct {
90+
BaseImage string
91+
Version string
92+
TargetArch string
93+
CommitSHA string
94+
BuildId string
95+
}
96+
97+
// ControllerImage - struct with info about controllers
98+
type Image struct {
99+
Tag string
100+
Digest string
101+
Registry string
102+
Name string
103+
}
104+
105+
func (i Image) print() string {
106+
return fmt.Sprintf("%s/%s:%s@sha256:%s", i.Registry, i.Name, i.Tag, i.Digest)
107+
}
108+
57109
func dockerBuild() error {
58-
/*
59-
docker build \
60-
${PLATFORM_FLAG} ${PLATFORM} \
61-
--no-cache \
62-
--pull \
63-
--build-arg BASE_IMAGE="$(BASE_IMAGE)" \
64-
--build-arg VERSION="$(TAG)" \
65-
--build-arg TARGETARCH="$(ARCH)" \
66-
--build-arg COMMIT_SHA="$(COMMIT_SHA)" \
67-
--build-arg BUILD_ID="$(BUILD_ID)" \
68-
-t $(REGISTRY)/controller:$(TAG) rootfs
69-
*/
70-
71-
cli, err := client.NewClientWithOpts(client.FromEnv)
72-
if err != nil {
73-
panic(err)
74-
}
75-
76-
builder := io.Reader{}
77-
78-
options := docker.ImageCreateOptions{
79-
80-
81-
}
82-
buildReponse, err := cli.ImageBuild(context.Background(), builder, options)
83-
if err != nil{
84-
return err
85-
}
86-
return nil
87-
88-
}
110+
/*
111+
docker build \
112+
${PLATFORM_FLAG} ${PLATFORM} \
113+
--no-cache \
114+
--pull \
115+
--build-arg BASE_IMAGE="$(BASE_IMAGE)" \
116+
--build-arg VERSION="$(TAG)" \
117+
--build-arg TARGETARCH="$(ARCH)" \
118+
--build-arg COMMIT_SHA="$(COMMIT_SHA)" \
119+
--build-arg BUILD_ID="$(BUILD_ID)" \
120+
-t $(REGISTRY)/controller:$(TAG) rootfs
121+
*/
122+
session := sh.NewSession()
123+
session.ShowCMD = true
124+
125+
fmt.Printf("Container Build Path: %v\n", dco.Path)
126+
127+
buildArgs := BuildArgs(dco.BuildArgs)
128+
129+
fmt.Printf("Base image: %s\n", dco.BuildArgs.BaseImage)
130+
131+
fmt.Printf("Build Args: %s\n", buildArgs)
132+
133+
session.Command("docker", "build", "--no-cache", "--pull", fmt.Sprintf("%v", buildArgs), fmt.Sprintf("%s", dco.Path)).Run()
134+
135+
return nil
136+
137+
}
138+
139+
func buildArgs(b *BuildArgs) string {
140+
141+
if b.BaseImage == "" {
142+
base, err := getIngressNginxBase()
143+
CheckIfError(err, "Issue Retrieving base image")
144+
fmt.Printf("Base Image set %s\n", base)
145+
b.BaseImage = base
146+
}
147+
148+
buildArgString := "--build-arg BASE_IMAGE=" + b.BaseImage
149+
150+
return buildArgString
151+
}
152+
153+
func getIngressNginxBase() (string, error) {
154+
fmt.Print("GET INGRESS NGINX BASE")
155+
dat, err := os.ReadFile("../NGINX_BASE")
156+
CheckIfError(err, "Could not read NGINX_BASE file")
157+
fmt.Printf("Get Ingress Dat: %v\n", dat)
158+
datString := string(dat)
159+
//remove newline
160+
datString = strings.Replace(datString, "\n", "", -1)
161+
return datString, nil
162+
}

ingressctl/cmd/tags.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package cmd
2+
3+
import (
4+
"os"
5+
"strings"
6+
)
7+
8+
func getIngressNGINXVersion() (string, error) {
9+
10+
dat, err := os.ReadFile("TAG")
11+
CheckIfError(err, "Could not read TAG file")
12+
datString := string(dat)
13+
//remove newline
14+
datString = strings.Replace(datString, "\n", "", -1)
15+
return datString, nil
16+
}

ingressctl/go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ module github.com/kubernetes/ingress-nginx/ingressctl
33
go 1.20
44

55
require (
6+
github.com/codeskyblue/go-sh v0.0.0-20200712050446-30169cf553fe
67
github.com/docker/docker v24.0.2+incompatible
78
github.com/spf13/cobra v1.7.0
89
)
910

1011
require (
1112
github.com/Microsoft/go-winio v0.6.1 // indirect
13+
github.com/codegangsta/inject v0.0.0-20150114235600-33e0aa1cb7c0 // indirect
1214
github.com/docker/distribution v2.8.2+incompatible // indirect
1315
github.com/docker/go-connections v0.4.0 // indirect
1416
github.com/docker/go-units v0.5.0 // indirect

ingressctl/go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 h1:UQHMgLO+TxOElx5B5HZ4hJQsoJ/PvUvKRhJHDQXO8P8=
22
github.com/Microsoft/go-winio v0.6.1 h1:9/kr64B9VUZrLm5YYwbGtUJnMgqWVOdUAXu6Migciow=
33
github.com/Microsoft/go-winio v0.6.1/go.mod h1:LRdKpFKfdobln8UmuiYcKPot9D2v6svN5+sAH+4kjUM=
4+
github.com/codegangsta/inject v0.0.0-20150114235600-33e0aa1cb7c0 h1:sDMmm+q/3+BukdIpxwO365v/Rbspp2Nt5XntgQRXq8Q=
5+
github.com/codegangsta/inject v0.0.0-20150114235600-33e0aa1cb7c0/go.mod h1:4Zcjuz89kmFXt9morQgcfYZAYZ5n8WHjt81YYWIwtTM=
6+
github.com/codeskyblue/go-sh v0.0.0-20200712050446-30169cf553fe h1:69JI97HlzP+PH5Mi1thcGlDoBr6PS2Oe+l3mNmAkbs4=
7+
github.com/codeskyblue/go-sh v0.0.0-20200712050446-30169cf553fe/go.mod h1:VQx0hjo2oUeQkQUET7wRwradO6f+fN5jzXgB/zROxxE=
48
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
59
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
610
github.com/docker/distribution v2.8.2+incompatible h1:T3de5rq0dB1j30rp0sA2rER+m322EBzniBPB6ZIzuh8=

ingressctl/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ import "github.com/kubernetes/ingress-nginx/ingressctl/cmd"
44

55
func main() {
66
cmd.Execute()
7+
78
}

0 commit comments

Comments
 (0)