Skip to content

Commit

Permalink
Merge pull request #154 from pwittrock/release-cmd
Browse files Browse the repository at this point in the history
Various improvements
  • Loading branch information
Phillip Wittrock committed Sep 23, 2017
2 parents e2de194 + d2050f9 commit 9abb982
Show file tree
Hide file tree
Showing 14 changed files with 362 additions and 48 deletions.
6 changes: 3 additions & 3 deletions cmd/apiregister-gen/generators/controller_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ import (
"strings"
"text/template"

"k8s.io/gengo/generator"
"github.com/markbates/inflect"
"k8s.io/gengo/generator"
)

type controllerGenerator struct {
Expand Down Expand Up @@ -130,7 +130,7 @@ func (c *{{.Target.Kind}}Controller) reconcile(key string) (err error) {
func (c *{{.Target.Kind}}Controller) Run(stopCh <-chan struct{}) {
c.queue.Run(stopCh)
c.controller.Run(stopCh)
controller.GetDefaults(c.controller).Run(stopCh)
}
`

Expand Down Expand Up @@ -226,7 +226,7 @@ func (d *informersGenerator) Imports(c *generator.Context) []string {
func (d *informersGenerator) Finalize(context *generator.Context, w io.Writer) error {
temp := template.Must(template.New("informersGenerator-template").Funcs(
template.FuncMap{
"title": strings.Title,
"title": strings.Title,
"plural": inflect.NewDefaultRuleset().Pluralize,
},
).Parse(InformersTemplate))
Expand Down
12 changes: 1 addition & 11 deletions cmd/apiserver-boot/boot/build/build_container.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,9 @@ limitations under the License.
package build

import (
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"strings"

"github.com/kubernetes-incubator/apiserver-builder/cmd/apiserver-boot/boot/util"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -80,14 +77,7 @@ func RunBuildContainer(cmd *cobra.Command, args []string) {

log.Printf("Building the docker Image using %s.", path)

c := exec.Command("docker", "build", "-t", Image, dir)
fmt.Printf("%s\n", strings.Join(c.Args, " "))
c.Stderr = os.Stderr
c.Stdout = os.Stdout
err = c.Run()
if err != nil {
log.Fatal(err)
}
util.DoCmd("docker", "build", "-t", Image, dir)
}

type dockerfileTemplateArguments struct {
Expand Down
92 changes: 83 additions & 9 deletions cmd/apiserver-boot/boot/build/build_resource_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ limitations under the License.
package build

import (
"bytes"
"encoding/base64"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"path"
"path/filepath"
"regexp"
Expand All @@ -40,15 +41,23 @@ var ControllerSecret string
var ControllerSecretMount string
var ControllerSecretEnv []string

var LocalMinikube bool
var LocalIp string

var buildResourceConfigCmd = &cobra.Command{
Use: "config",
Short: "Create kubernetes resource config files to launch the apiserver.",
Long: `Create kubernetes resource config files to launch the apiserver.`,
Example: `
# Build yaml resource config into the config/ directory for running the apiserver and
# controller-manager as an aggregated service in a Kubernetes cluster
# controller-manager as an aggregated service in a Kubernetes cluster as a container.
# Generates CA and apiserver certificates.
apiserver-boot build config --name nameofservice --namespace mysystemnamespace --image gcr.io/myrepo/myimage:mytag
# Build yaml resource config into the config/ directory for running the apiserver and
# controller-manager locally, but registered through aggregation into a local minikube cluster
# Generates CA and apiserver certificates.
apiserver-boot build config --name nameofservice --namespace mysystemnamespace --local-minikube
`,
Run: RunBuildResourceConfig,
}
Expand All @@ -68,6 +77,9 @@ func AddBuildResourceConfigFlags(cmd *cobra.Command) {
cmd.Flags().StringVar(&Namespace, "namespace", "", "")
cmd.Flags().StringVar(&Image, "image", "", "name of the apiserver Image with tag")
cmd.Flags().StringVar(&ResourceConfigDir, "output", "config", "directory to output resourceconfig")

cmd.Flags().BoolVar(&LocalMinikube, "local-minikube", false, "if true, generate config to run locally but aggregate through minikube.")
cmd.Flags().StringVar(&LocalIp, "local-ip", "10.0.2.2", "if using --local-minikube, this is the ip address minikube will look for the aggregated server at.")
}

func RunBuildResourceConfig(cmd *cobra.Command, args []string) {
Expand All @@ -77,7 +89,7 @@ func RunBuildResourceConfig(cmd *cobra.Command, args []string) {
if len(Namespace) == 0 {
log.Fatalf("must specify --namespace")
}
if len(Image) == 0 {
if len(Image) == 0 && !LocalMinikube {
log.Fatalf("Must specify --image")
}
util.GetDomain()
Expand All @@ -91,12 +103,31 @@ func RunBuildResourceConfig(cmd *cobra.Command, args []string) {
}

func getBase64(file string) string {
out, err := exec.Command("bash", "-c",
fmt.Sprintf("base64 %s | awk 'BEGIN{ORS=\"\";} {print}'", file)).CombinedOutput()
//out, err := exec.Command("bash", "-c",
// fmt.Sprintf("base64 %s | awk 'BEGIN{ORS=\"\";} {print}'", file)).CombinedOutput()
//if err != nil {
// log.Fatalf("Could not base64 encode file: %v", err)
//}

buff := bytes.Buffer{}
enc := base64.NewEncoder(base64.StdEncoding, &buff)
data, err := ioutil.ReadFile(file)
if err != nil {
log.Fatalf("Could not base64 encode file: %v", err)
log.Fatalf("Could not read file %s: %v", file, err)
}
return string(out)

_, err = enc.Write(data)
if err != nil {
log.Fatalf("Could not write bytes: %v", err)
}
enc.Close()
return buff.String()

//if string(out) != buff.String() {
// fmt.Printf("\nNot Equal\n")
//}
//
//return string(out)
}

func buildResourceConfig() {
Expand All @@ -117,18 +148,23 @@ func buildResourceConfig() {
ControllerSecretMount: ControllerSecretMount,
ControllerSecret: ControllerSecret,
ControllerSecretEnv: ControllerSecretEnv,
LocalIp: LocalIp,
}
path := filepath.Join(ResourceConfigDir, "apiserver.yaml")

created := util.WriteIfNotFound(path, "config-template", resourceConfigTemplate, a)
temp := resourceConfigTemplate
if LocalMinikube {
temp = localConfigTemplate
}
created := util.WriteIfNotFound(path, "config-template", temp, a)
if !created {
log.Fatalf("Resource config already exists.")
}
}

func createCerts() {
dir := filepath.Join(ResourceConfigDir, "certificates")
util.DoCmd("mkdir", "-p", dir)
os.MkdirAll(dir, 0700)

if _, err := os.Stat(filepath.Join(dir, "apiserver_ca.crt")); os.IsNotExist(err) {
util.DoCmd("openssl", "req", "-x509",
Expand Down Expand Up @@ -218,6 +254,7 @@ type resourceConfigTemplateArgs struct {
ControllerSecret string
ControllerSecretMount string
ControllerSecretEnv []string
LocalIp string
}

var resourceConfigTemplate = `
Expand Down Expand Up @@ -412,5 +449,42 @@ metadata:
data:
tls.crt: {{ .ClientCert }}
tls.key: {{ .ClientKey }}
`

var localConfigTemplate = `
{{ $config := . -}}
{{ range $api := .Versions -}}
apiVersion: apiregistration.k8s.io/v1beta1
kind: APIService
metadata:
name: {{ $api.Version }}.{{ $api.Group }}.{{ $config.Domain }}
labels:
api: {{ $config.Name }}
apiserver: "true"
spec:
version: {{ $api.Version }}
group: {{ $api.Group }}.{{ $config.Domain }}
groupPriorityMinimum: 2000
priority: 200
service:
name: {{ $config.Name }}
namespace: {{ $config.Namespace }}
versionPriority: 10
caBundle: "{{ $config.CACert }}"
---
{{ end -}}
apiVersion: v1
kind: Service
metadata:
name: {{.Name}}
namespace: {{.Namespace}}
labels:
api: {{.Name}}
apiserver: "true"
spec:
type: ExternalName
externalName: "{{ .LocalIp }}"
ports:
- port: 443
protocol: TCP
`
6 changes: 3 additions & 3 deletions cmd/apiserver-boot/boot/build/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,9 @@ func RunDocs(cmd *cobra.Command, args []string) {
}

os.RemoveAll(filepath.Join(outputDir, "includes"))
exec.Command("mkdir", "-p", filepath.Join(outputDir, "openapi-spec")).CombinedOutput()
exec.Command("mkdir", "-p", filepath.Join(outputDir, "static_includes")).CombinedOutput()
exec.Command("mkdir", "-p", filepath.Join(outputDir, "examples")).CombinedOutput()
os.MkdirAll(filepath.Join(outputDir, "openapi-spec"), 0700)
os.MkdirAll(filepath.Join(outputDir, "static_includes"), 0700)
os.MkdirAll(filepath.Join(outputDir, "examples"), 0700)

// Build the swagger.json
if buildOpenapi {
Expand Down
5 changes: 2 additions & 3 deletions cmd/apiserver-boot/boot/create/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"github.com/kubernetes-incubator/apiserver-builder/cmd/apiserver-boot/boot/util"
"github.com/markbates/inflect"
"github.com/spf13/cobra"
"os/exec"
)

var kindName string
Expand Down Expand Up @@ -99,7 +98,7 @@ func createResource(boilerplate string) {
}
}

exec.Command("mkdir", "-p", filepath.Join("docs", "examples")).CombinedOutput()
os.MkdirAll(filepath.Join("docs", "examples"), 0700)
docpath := filepath.Join("docs", "examples", strings.ToLower(kindName), fmt.Sprintf("%s.yaml", strings.ToLower(kindName)))
created = util.WriteIfNotFound(docpath, "example-template", exampleTemplate, a)
if !created {
Expand All @@ -109,7 +108,7 @@ func createResource(boilerplate string) {
}
}

exec.Command("mkdir", "-p", filepath.Join("sample")).CombinedOutput()
os.MkdirAll("sample", 0700)
samplepath := filepath.Join("sample", fmt.Sprintf("%s.yaml", strings.ToLower(kindName)))
created = util.WriteIfNotFound(samplepath, "sample-template", sampleTemplate, a)
if !created {
Expand Down
44 changes: 34 additions & 10 deletions cmd/apiserver-boot/boot/init_repo/glide.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ limitations under the License.
package init_repo

import (
"archive/tar"
"compress/gzip"
"log"
"os"
"os/exec"
Expand All @@ -25,6 +27,7 @@ import (

"github.com/kubernetes-incubator/apiserver-builder/cmd/apiserver-boot/boot/util"
"github.com/spf13/cobra"
"io/ioutil"
)

var glideInstallCmd = &cobra.Command{
Expand Down Expand Up @@ -104,26 +107,47 @@ func fetchGlide() {
func copyGlide() {
// Move up two directories from the location of the `apiserver-boot`
// executable to find the `vendor` directory we package with our
// releases. TODO(campbellalex@google.com): this doesn't work for people
// who used `go install` to put `apiserver-boot` in their $GOPATH/bin.
// releases.
e, err := os.Executable()
if err != nil {
log.Fatal("unable to get directory of apiserver-builder tools")
}

e = filepath.Dir(filepath.Dir(e))

doCmd := func(cmd string, args ...string) {
c := exec.Command(cmd, args...)
c.Stderr = os.Stderr
c.Stdout = os.Stdout
err = c.Run()
// read the file
f := filepath.Join(e, "bin", "glide.tar.gz")
fr, err := os.Open(f)
if err != nil {
log.Fatalf("failed to read vendor tar file %s %v", f, err)
}
defer fr.Close()

// setup gzip of tar
gr, err := gzip.NewReader(fr)
if err != nil {
log.Fatalf("failed to read vendor tar file %s %v", f, err)
}
defer gr.Close()

// setup tar reader
tr := tar.NewReader(gr)

for file, err := tr.Next(); err == nil; file, err = tr.Next() {
p := filepath.Join(".", file.Name)
err := os.MkdirAll(filepath.Dir(p), 0700)
if err != nil {
log.Fatalf("Could not create directory %s: %v", filepath.Dir(p), err)
}
b, err := ioutil.ReadAll(tr)
if err != nil {
log.Fatalf("failed to copy go dependencies %v", err)
log.Fatalf("Could not read file %s: %v", file.Name, err)
}
err = ioutil.WriteFile(p, b, os.FileMode(file.Mode))
if err != nil {
log.Fatalf("Could not write file %s: %v", p, err)
}
}

doCmd("tar", "-xzvf", filepath.Join(e, "bin", "glide.tar.gz"))
}

func RunGlideInstall(cmd *cobra.Command, args []string) {
Expand Down
3 changes: 1 addition & 2 deletions cmd/apiserver-boot/boot/init_repo/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package init_repo
import (
"log"
"os"
"os/exec"
"path/filepath"

"github.com/kubernetes-incubator/apiserver-builder/cmd/apiserver-boot/boot/util"
Expand Down Expand Up @@ -64,7 +63,7 @@ func RunInitRepo(cmd *cobra.Command, args []string) {
createPackage(cr, filepath.Join("pkg", "controller", "sharedinformers"))
createPackage(cr, filepath.Join("pkg", "openapi"))

exec.Command("mkdir", "-p", filepath.Join("bin")).CombinedOutput()
os.MkdirAll("bin", 0700)

if installDeps {
log.Printf("installing godeps. To disable this, run with --install-deps=false.")
Expand Down
Loading

0 comments on commit 9abb982

Please sign in to comment.