<commit_before>/*
Copyright 2011 Google Inc.

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

import (
	"fmt"
	"http"
	"os"
	"log"
	"strings"

	"camli/blobserver"
)

var _ = log.Printf

type SyncHandler struct {
	fromName, toName string
	from, fromq, to  blobserver.Storage
}

func createSyncHandler(fromName, toName string, from, to blobserver.Storage) (*SyncHandler, os.Error) {
	h := &SyncHandler{
		from:     from,
		to:       to,
		fromName: fromName,
		toName:   toName,
	}

	qc, ok := from.(blobserver.QueueCreator)
	if !ok {
		return nil, fmt.Errorf(
			"Prefix %s (type %T) does not support being efficient replication source (queueing)",
			fromName, from)
	}
	queueName := strings.Replace(strings.Trim(toName, "/"), "/", "-", -1)
	var err os.Error
	h.fromq, err = qc.CreateQueue(queueName)
	if err != nil {
		return nil, fmt.Errorf("Prefix %s (type %T) failed to create queue %q: %v",
			fromName, from, queueName, err)
	}

	return h, nil
}

func (sh *SyncHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
	fmt.Fprintf(rw, "sync handler, from %s to %s", sh.fromName, sh.toName)
}
<commit_msg>real-time replication built-in to camlistored works now.<commit_after>/*
Copyright 2011 Google Inc.

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

import (
	"fmt"
	"html"
	"http"
	"os"
	"log"
	"strings"
	"sync"
	"time"

	"camli/blobref"
	"camli/blobserver"
)

const queueSyncInterval = seconds(5)
const maxErrors = 20

var _ = log.Printf

type SyncHandler struct {
	fromName, fromqName, toName string
	from, fromq, to             blobserver.Storage

	lk           sync.Mutex
	lastStatus   string
	recentErrors []timestampedError
}

type timestampedError struct {
	t   *time.Time
	err os.Error
}

func createSyncHandler(fromName, toName string, from, to blobserver.Storage) (*SyncHandler, os.Error) {
	h := &SyncHandler{
		from:       from,
		to:         to,
		fromName:   fromName,
		toName:     toName,
		lastStatus: "not started",
	}

	qc, ok := from.(blobserver.QueueCreator)
	if !ok {
		return nil, fmt.Errorf(
			"Prefix %s (type %T) does not support being efficient replication source (queueing)",
			fromName, from)
	}
	h.fromqName = strings.Replace(strings.Trim(toName, "/"), "/", "-", -1)
	var err os.Error
	h.fromq, err = qc.CreateQueue(h.fromqName)
	if err != nil {
		return nil, fmt.Errorf("Prefix %s (type %T) failed to create queue %q: %v",
			fromName, from, h.fromqName, err)
	}

	go h.syncQueueLoop()

	return h, nil
}

func (sh *SyncHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
	fmt.Fprintf(rw, "sync handler, from %s to %s
status: %s", sh.fromName, sh.toName,
		html.EscapeString(sh.status()))

	sh.lk.Lock()
	defer sh.lk.Unlock()
	if len(sh.recentErrors) > 0 {
		fmt.Fprintf(rw, "
Recent Errors:

")
		for _, te := range sh.recentErrors {
			fmt.Fprintf(rw, "
%s: %s
\n",
				te.t.Format(time.RFC3339),
				html.EscapeString(te.err.String()))
		}
		fmt.Fprintf(rw, "
")
	}
}

func (sh *SyncHandler) setStatus(s string, args ...interface{}) {
	s = time.UTC().Format(time.RFC3339) + ": " + fmt.Sprintf(s, args...)
	sh.lk.Lock()
	defer sh.lk.Unlock()
	sh.lastStatus = s
}

func (sh *SyncHandler) status() string {
	sh.lk.Lock()
	defer sh.lk.Unlock()
	return sh.lastStatus
}

func (sh *SyncHandler) addErrorToLog(err os.Error) {
	log.Printf(err.String())
	sh.lk.Lock()
	defer sh.lk.Unlock()
	sh.recentErrors = append(sh.recentErrors, timestampedError{time.UTC(), err})
	if len(sh.recentErrors) > maxErrors {
		// Kinda lame, but whatever. Only for errors, rare.
		copy(sh.recentErrors[:maxErrors], sh.recentErrors[1:maxErrors+1])
		sh.recentErrors = sh.recentErrors[:maxErrors]
	}
}

func (sh *SyncHandler) syncQueueLoop() {
	every(queueSyncInterval, func() {
		sh.setStatus("Long-polling enumerate on queue %q, waiting for new blobs.", sh.fromqName)

		ch := make(chan blobref.SizedBlobRef)
		errch := make(chan os.Error, 1)
		go func() {
			log.Printf("pre-enumerate, for %d seconds", int(queueSyncInterval.Seconds()))
			errch <- sh.fromq.EnumerateBlobs(ch, "", 100, int(queueSyncInterval.Seconds()))
			log.Printf("post-enumerate")
		}()
		for sb := range ch {
			log.Printf("sync in queue %q: got blob: %s", sh.fromqName, sb)

			// TODO: have a pool of copiers, not just a
			// single thread here.  Mostly simple, but
			// having a good status will make it more
			// complicated.

			error := func(s string, args ...interface{}) {
				// TODO: increment error stats
				pargs := []interface{}{sh.fromqName, sb.BlobRef}
				pargs = append(pargs, args...)
				sh.addErrorToLog(fmt.Errorf("replication error for queue %q, blob %s: "+s, pargs...))
			}

			sh.setStatus("Syncing blob %s (size %d)", sb.BlobRef, sb.Size)
			blobReader, fromSize, err := sh.from.FetchStreaming(sb.BlobRef)
			if err != nil {
				error("source fetch: %v", err)
				continue
			}
			if fromSize != sb.Size {
				error("source fetch size mismatch: get=%d, enumerate=%d", fromSize, sb.Size)
				continue
			}
			newsb, err := sh.to.ReceiveBlob(sb.BlobRef, blobReader)
			if err != nil {
				error("dest write: %v", err)
				continue
			}
			if newsb.Size != sb.Size {
				error("write size mismatch: source_read=%d but dest_write=%d", sb.Size, newsb.Size)
				continue
			}
			err = sh.fromq.Remove([]*blobref.BlobRef{sb.BlobRef})
			if err != nil {
				error("source queue delete: %v", err)
			}
			error("replicated %s size %d", sb.BlobRef, sb.Size)
		}
		if err := <-errch; err != nil {
			sh.addErrorToLog(fmt.Errorf("replication error for queue %q, enumerate from source: %v", err))
			return
		}

		sh.setStatus("Sleeping briefly before next long poll.")
	})
}

// TODO: move this elsewhere (timeutil?)
func every(interval nanoer, f func()) {
	nsInterval := int64(interval.Nanos())
	for {
		t1 := time.Nanoseconds()
		f()
		if sleep := (t1 + nsInterval) - time.Nanoseconds(); sleep > 0 {
			time.Sleep(sleep)
		}
	}
}

// TODO: move this time stuff elsewhere
type seconds int64
type nanos int64

func (s seconds) Nanos() nanos {
	return nanos(int64(s) * 1e9)
}

func (s seconds) Seconds() seconds {
	return s
}

type nanoer interface {
	Nanos() nanos
}
<|endoftext|>


<commit_before>// Copyright 2018 syzkaller project authors. All rights reserved.
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.

// Package build contains helper functions for building kernels/images.
package build

import (
	"bytes"
	"fmt"
	"os"
	"path/filepath"
	"regexp"
	"strings"
	"time"

	"github.com/google/syzkaller/pkg/osutil"
	"github.com/google/syzkaller/pkg/report"
	"github.com/google/syzkaller/pkg/vcs"
	"github.com/google/syzkaller/sys/targets"
)

// Params is input arguments for the Image function.
type Params struct {
	TargetOS     string
	TargetArch   string
	VMType       string
	KernelDir    string
	OutputDir    string
	Compiler     string
	Ccache       string
	UserspaceDir string
	CmdlineFile  string
	SysctlFile   string
	Config       []byte
}

// Information that is returned from the Image function.
type ImageDetails struct {
	Signature  string
	CompilerID string
}

// Image creates a disk image for the specified OS/ARCH/VM.
// Kernel is taken from KernelDir, userspace system is taken from UserspaceDir.
// If CmdlineFile is not empty, contents of the file are appended to the kernel command line.
// If SysctlFile is not empty, contents of the file are appended to the image /etc/sysctl.conf.
// Output is stored in OutputDir and includes (everything except for image is optional):
//  - image: the image
//  - key: ssh key for the image
//  - kernel: kernel for injected boot
//  - initrd: initrd for injected boot
//  - kernel.config: actual kernel config used during build
//  - obj/: directory with kernel object files (this should match KernelObject
//    specified in sys/targets, e.g. vmlinux for linux)
// The returned structure contains a kernel ID that will be the same for kernels
// with the same runtime behavior, and different for kernels with different runtime
// behavior. Binary equal builds, or builds that differ only in e.g. debug info,
// have the same ID. The ID may be empty if OS implementation does not have
// a way to calculate such IDs.
// Also that structure provides a compiler ID field that contains the name and
// the version of the compiler/toolchain that was used to build the kernel.
// The CompilerID field is not guaranteed to be non-empty.
func Image(params Params) (details ImageDetails, err error) {
	builder, err := getBuilder(params.TargetOS, params.TargetArch, params.VMType)
	if err != nil {
		return
	}
	if err = osutil.MkdirAll(filepath.Join(params.OutputDir, "obj")); err != nil {
		return
	}
	if len(params.Config) != 0 {
		// Write kernel config early, so that it's captured on build failures.
		if err = osutil.WriteFile(filepath.Join(params.OutputDir, "kernel.config"), params.Config); err != nil {
			err = fmt.Errorf("failed to write config file: %v", err)
			return
		}
	}
	details, err = builder.build(params)
	if details.CompilerID == "" {
		// Fill in the compiler info even if the build failed.
		var idErr error
		details.CompilerID, idErr = compilerIdentity(params.Compiler)
		if err == nil {
			err = idErr
		} // Try to preserve the build error otherwise.
	}
	if err != nil {
		err = extractRootCause(err, params.TargetOS, params.KernelDir)
		return
	}
	if key := filepath.Join(params.OutputDir, "key"); osutil.IsExist(key) {
		if err := os.Chmod(key, 0600); err != nil {
			return details, fmt.Errorf("failed to chmod 0600 %v: %v", key, err)
		}
	}
	return
}

func Clean(targetOS, targetArch, vmType, kernelDir string) error {
	builder, err := getBuilder(targetOS, targetArch, vmType)
	if err != nil {
		return err
	}
	return builder.clean(kernelDir, targetArch)
}

type KernelError struct {
	Report     []byte
	Output     []byte
	Recipients vcs.Recipients
	guiltyFile string
}

func (err *KernelError) Error() string {
	return string(err.Report)
}

type builder interface {
	build(params Params) (ImageDetails, error)
	clean(kernelDir, targetArch string) error
}

func getBuilder(targetOS, targetArch, vmType string) (builder, error) {
	var supported = []struct {
		OS    string
		archs []string
		vms   []string
		b     builder
	}{
		{targets.Linux, []string{targets.AMD64}, []string{"gvisor"}, gvisor{}},
		{targets.Linux, []string{targets.AMD64}, []string{"gce", "qemu"}, linux{}},
		{targets.Linux, []string{targets.ARM, targets.ARM64, targets.I386, targets.MIPS64LE,
			targets.PPC64LE, targets.S390x, targets.RiscV64}, []string{"qemu"}, linux{}},
		{targets.Fuchsia, []string{targets.AMD64, targets.ARM64}, []string{"qemu"}, fuchsia{}},
		{targets.Akaros, []string{targets.AMD64}, []string{"qemu"}, akaros{}},
		{targets.OpenBSD, []string{targets.AMD64}, []string{"gce", "vmm"}, openbsd{}},
		{targets.NetBSD, []string{targets.AMD64}, []string{"gce", "qemu"}, netbsd{}},
		{targets.FreeBSD, []string{targets.AMD64}, []string{"gce", "qemu"}, freebsd{}},
		{targets.Darwin, []string{targets.AMD64}, []string{"qemu"}, darwin{}},
		{targets.TestOS, []string{targets.TestArch64}, []string{"qemu"}, test{}},
	}
	for _, s := range supported {
		if targetOS == s.OS {
			for _, arch := range s.archs {
				if targetArch == arch {
					for _, vm := range s.vms {
						if vmType == vm {
							return s.b, nil
						}
					}
				}
			}
		}
	}
	return nil, fmt.Errorf("unsupported image type %v/%v/%v", targetOS, targetArch, vmType)
}

func compilerIdentity(compiler string) (string, error) {
	if compiler == "" {
		return "", nil
	}

	bazel := strings.HasSuffix(compiler, "bazel")

	arg := "--version"
	if bazel {
		arg = ""
	}
	output, err := osutil.RunCmd(time.Minute, "", compiler, arg)
	if err != nil {
		return "", err
	}
	for _, line := range strings.Split(string(output), "\n") {
		if bazel {
			// Strip extracting and log lines...
			if strings.Contains(line, "Extracting Bazel") {
				continue
			}
			if strings.HasPrefix(line, "INFO: ") {
				continue
			}
			if strings.HasPrefix(line, "WARNING: ") {
				continue
			}
		}

		return strings.TrimSpace(line), nil
	}
	return "", fmt.Errorf("no output from compiler --version")
}

func extractRootCause(err error, OS, kernelSrc string) error {
	if err == nil {
		return nil
	}
	verr, ok := err.(*osutil.VerboseError)
	if !ok {
		return err
	}
	reason, file := extractCauseInner(verr.Output, kernelSrc)
	if len(reason) == 0 {
		return err
	}
	kernelErr := &KernelError{
		Report:     reason,
		Output:     verr.Output,
		guiltyFile: file,
	}
	if file != "" && OS == targets.Linux {
		maintainers, err := report.GetLinuxMaintainers(kernelSrc, file)
		if err != nil {
			kernelErr.Output = append(kernelErr.Output, err.Error()...)
		}
		kernelErr.Recipients = maintainers
	}
	return kernelErr
}

func extractCauseInner(s []byte, kernelSrc string) ([]byte, string) {
	lines := extractCauseRaw(s)
	const maxLines = 20
	if len(lines) > maxLines {
		lines = lines[:maxLines]
	}
	var stripPrefix []byte
	if kernelSrc != "" {
		stripPrefix = []byte(kernelSrc)
		if stripPrefix[len(stripPrefix)-1] != filepath.Separator {
			stripPrefix = append(stripPrefix, filepath.Separator)
		}
	}
	file := ""
	for i := range lines {
		if stripPrefix != nil {
			lines[i] = bytes.Replace(lines[i], stripPrefix, nil, -1)
		}
		if file == "" {
			for _, fileRe := range fileRes {
				match := fileRe.FindSubmatch(lines[i])
				if match != nil {
					file = string(match[1])
					if file[0] != '/' {
						break
					}
					// We already removed kernel source prefix,
					// if we still have an absolute path, it's probably pointing
					// to compiler/system libraries (not going to work).
					file = ""
				}
			}
		}
	}
	file = strings.TrimPrefix(file, "./")
	if strings.HasSuffix(file, ".o") {
		// Linker may point to object files instead.
		file = strings.TrimSuffix(file, ".o") + ".c"
	}
	res := bytes.Join(lines, []byte{'\n'})
	// gcc uses these weird quotes around identifiers, which may be
	// mis-rendered by systems that don't understand utf-8.
	res = bytes.Replace(res, []byte("‘"), []byte{'\''}, -1)
	res = bytes.Replace(res, []byte("’"), []byte{'\''}, -1)
	return res, file
}

func extractCauseRaw(s []byte) [][]byte {
	weak := true
	var cause [][]byte
	dedup := make(map[string]bool)
	for _, line := range bytes.Split(s, []byte{'\n'}) {
		for _, pattern := range buildFailureCauses {
			if !pattern.pattern.Match(line) {
				continue
			}
			if weak && !pattern.weak {
				cause = nil
				dedup = make(map[string]bool)
			}
			if dedup[string(line)] {
				continue
			}
			dedup[string(line)] = true
			if cause == nil {
				weak = pattern.weak
			}
			cause = append(cause, line)
			break
		}
	}
	return cause
}

type buildFailureCause struct {
	pattern *regexp.Regexp
	weak    bool
}

var buildFailureCauses = [...]buildFailureCause{
	{pattern: regexp.MustCompile(`: error: `)},
	{pattern: regexp.MustCompile(`ERROR: `)},
	{pattern: regexp.MustCompile(`: fatal error: `)},
	{pattern: regexp.MustCompile(`: undefined reference to`)},
	{pattern: regexp.MustCompile(`: multiple definition of`)},
	{pattern: regexp.MustCompile(`: Permission denied`)},
	{pattern: regexp.MustCompile(`: not found`)},
	{pattern: regexp.MustCompile(`^([a-zA-Z0-9_\-/.]+):[0-9]+:([0-9]+:)?.*(error|invalid|fatal|wrong)`)},
	{pattern: regexp.MustCompile(`FAILED unresolved symbol`)},
	{pattern: regexp.MustCompile(`No rule to make target`)},
	{weak: true, pattern: regexp.MustCompile(`: final link failed: `)},
	{weak: true, pattern: regexp.MustCompile(`collect2: error: `)},
	{weak: true, pattern: regexp.MustCompile(`FAILED: Build did NOT complete`)},
}

var fileRes = []*regexp.Regexp{
	regexp.MustCompile(`^([a-zA-Z0-9_\-/.]+):[0-9]+:([0-9]+:)? `),
	regexp.MustCompile(`^(?:ld: )?(([a-zA-Z0-9_\-/.]+?)\.o):`),
	regexp.MustCompile(`; (([a-zA-Z0-9_\-/.]+?)\.o):`),
}
<commit_msg>pkg/build: increse timeout for bazel<commit_after>// Copyright 2018 syzkaller project authors. All rights reserved.
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.

// Package build contains helper functions for building kernels/images.
package build

import (
	"bytes"
	"fmt"
	"os"
	"path/filepath"
	"regexp"
	"strings"
	"time"

	"github.com/google/syzkaller/pkg/osutil"
	"github.com/google/syzkaller/pkg/report"
	"github.com/google/syzkaller/pkg/vcs"
	"github.com/google/syzkaller/sys/targets"
)

// Params is input arguments for the Image function.
type Params struct {
	TargetOS     string
	TargetArch   string
	VMType       string
	KernelDir    string
	OutputDir    string
	Compiler     string
	Ccache       string
	UserspaceDir string
	CmdlineFile  string
	SysctlFile   string
	Config       []byte
}

// Information that is returned from the Image function.
type ImageDetails struct {
	Signature  string
	CompilerID string
}

// Image creates a disk image for the specified OS/ARCH/VM.
// Kernel is taken from KernelDir, userspace system is taken from UserspaceDir.
// If CmdlineFile is not empty, contents of the file are appended to the kernel command line.
// If SysctlFile is not empty, contents of the file are appended to the image /etc/sysctl.conf.
// Output is stored in OutputDir and includes (everything except for image is optional):
//  - image: the image
//  - key: ssh key for the image
//  - kernel: kernel for injected boot
//  - initrd: initrd for injected boot
//  - kernel.config: actual kernel config used during build
//  - obj/: directory with kernel object files (this should match KernelObject
//    specified in sys/targets, e.g. vmlinux for linux)
// The returned structure contains a kernel ID that will be the same for kernels
// with the same runtime behavior, and different for kernels with different runtime
// behavior. Binary equal builds, or builds that differ only in e.g. debug info,
// have the same ID. The ID may be empty if OS implementation does not have
// a way to calculate such IDs.
// Also that structure provides a compiler ID field that contains the name and
// the version of the compiler/toolchain that was used to build the kernel.
// The CompilerID field is not guaranteed to be non-empty.
func Image(params Params) (details ImageDetails, err error) {
	builder, err := getBuilder(params.TargetOS, params.TargetArch, params.VMType)
	if err != nil {
		return
	}
	if err = osutil.MkdirAll(filepath.Join(params.OutputDir, "obj")); err != nil {
		return
	}
	if len(params.Config) != 0 {
		// Write kernel config early, so that it's captured on build failures.
		if err = osutil.WriteFile(filepath.Join(params.OutputDir, "kernel.config"), params.Config); err != nil {
			err = fmt.Errorf("failed to write config file: %v", err)
			return
		}
	}
	details, err = builder.build(params)
	if details.CompilerID == "" {
		// Fill in the compiler info even if the build failed.
		var idErr error
		details.CompilerID, idErr = compilerIdentity(params.Compiler)
		if err == nil {
			err = idErr
		} // Try to preserve the build error otherwise.
	}
	if err != nil {
		err = extractRootCause(err, params.TargetOS, params.KernelDir)
		return
	}
	if key := filepath.Join(params.OutputDir, "key"); osutil.IsExist(key) {
		if err := os.Chmod(key, 0600); err != nil {
			return details, fmt.Errorf("failed to chmod 0600 %v: %v", key, err)
		}
	}
	return
}

func Clean(targetOS, targetArch, vmType, kernelDir string) error {
	builder, err := getBuilder(targetOS, targetArch, vmType)
	if err != nil {
		return err
	}
	return builder.clean(kernelDir, targetArch)
}

type KernelError struct {
	Report     []byte
	Output     []byte
	Recipients vcs.Recipients
	guiltyFile string
}

func (err *KernelError) Error() string {
	return string(err.Report)
}

type builder interface {
	build(params Params) (ImageDetails, error)
	clean(kernelDir, targetArch string) error
}

func getBuilder(targetOS, targetArch, vmType string) (builder, error) {
	var supported = []struct {
		OS    string
		archs []string
		vms   []string
		b     builder
	}{
		{targets.Linux, []string{targets.AMD64}, []string{"gvisor"}, gvisor{}},
		{targets.Linux, []string{targets.AMD64}, []string{"gce", "qemu"}, linux{}},
		{targets.Linux, []string{targets.ARM, targets.ARM64, targets.I386, targets.MIPS64LE,
			targets.PPC64LE, targets.S390x, targets.RiscV64}, []string{"qemu"}, linux{}},
		{targets.Fuchsia, []string{targets.AMD64, targets.ARM64}, []string{"qemu"}, fuchsia{}},
		{targets.Akaros, []string{targets.AMD64}, []string{"qemu"}, akaros{}},
		{targets.OpenBSD, []string{targets.AMD64}, []string{"gce", "vmm"}, openbsd{}},
		{targets.NetBSD, []string{targets.AMD64}, []string{"gce", "qemu"}, netbsd{}},
		{targets.FreeBSD, []string{targets.AMD64}, []string{"gce", "qemu"}, freebsd{}},
		{targets.Darwin, []string{targets.AMD64}, []string{"qemu"}, darwin{}},
		{targets.TestOS, []string{targets.TestArch64}, []string{"qemu"}, test{}},
	}
	for _, s := range supported {
		if targetOS == s.OS {
			for _, arch := range s.archs {
				if targetArch == arch {
					for _, vm := range s.vms {
						if vmType == vm {
							return s.b, nil
						}
					}
				}
			}
		}
	}
	return nil, fmt.Errorf("unsupported image type %v/%v/%v", targetOS, targetArch, vmType)
}

func compilerIdentity(compiler string) (string, error) {
	if compiler == "" {
		return "", nil
	}

	bazel := strings.HasSuffix(compiler, "bazel")

	arg, timeout := "--version", time.Minute
	if bazel {
		// Bazel episodically fails with 1 min timeout.
		arg, timeout = "", 10*time.Minute
	}
	output, err := osutil.RunCmd(timeout, "", compiler, arg)
	if err != nil {
		return "", err
	}
	for _, line := range strings.Split(string(output), "\n") {
		if bazel {
			// Strip extracting and log lines...
			if strings.Contains(line, "Extracting Bazel") {
				continue
			}
			if strings.HasPrefix(line, "INFO: ") {
				continue
			}
			if strings.HasPrefix(line, "WARNING: ") {
				continue
			}
		}

		return strings.TrimSpace(line), nil
	}
	return "", fmt.Errorf("no output from compiler --version")
}

func extractRootCause(err error, OS, kernelSrc string) error {
	if err == nil {
		return nil
	}
	verr, ok := err.(*osutil.VerboseError)
	if !ok {
		return err
	}
	reason, file := extractCauseInner(verr.Output, kernelSrc)
	if len(reason) == 0 {
		return err
	}
	kernelErr := &KernelError{
		Report:     reason,
		Output:     verr.Output,
		guiltyFile: file,
	}
	if file != "" && OS == targets.Linux {
		maintainers, err := report.GetLinuxMaintainers(kernelSrc, file)
		if err != nil {
			kernelErr.Output = append(kernelErr.Output, err.Error()...)
		}
		kernelErr.Recipients = maintainers
	}
	return kernelErr
}

func extractCauseInner(s []byte, kernelSrc string) ([]byte, string) {
	lines := extractCauseRaw(s)
	const maxLines = 20
	if len(lines) > maxLines {
		lines = lines[:maxLines]
	}
	var stripPrefix []byte
	if kernelSrc != "" {
		stripPrefix = []byte(kernelSrc)
		if stripPrefix[len(stripPrefix)-1] != filepath.Separator {
			stripPrefix = append(stripPrefix, filepath.Separator)
		}
	}
	file := ""
	for i := range lines {
		if stripPrefix != nil {
			lines[i] = bytes.Replace(lines[i], stripPrefix, nil, -1)
		}
		if file == "" {
			for _, fileRe := range fileRes {
				match := fileRe.FindSubmatch(lines[i])
				if match != nil {
					file = string(match[1])
					if file[0] != '/' {
						break
					}
					// We already removed kernel source prefix,
					// if we still have an absolute path, it's probably pointing
					// to compiler/system libraries (not going to work).
					file = ""
				}
			}
		}
	}
	file = strings.TrimPrefix(file, "./")
	if strings.HasSuffix(file, ".o") {
		// Linker may point to object files instead.
		file = strings.TrimSuffix(file, ".o") + ".c"
	}
	res := bytes.Join(lines, []byte{'\n'})
	// gcc uses these weird quotes around identifiers, which may be
	// mis-rendered by systems that don't understand utf-8.
	res = bytes.Replace(res, []byte("‘"), []byte{'\''}, -1)
	res = bytes.Replace(res, []byte("’"), []byte{'\''}, -1)
	return res, file
}

func extractCauseRaw(s []byte) [][]byte {
	weak := true
	var cause [][]byte
	dedup := make(map[string]bool)
	for _, line := range bytes.Split(s, []byte{'\n'}) {
		for _, pattern := range buildFailureCauses {
			if !pattern.pattern.Match(line) {
				continue
			}
			if weak && !pattern.weak {
				cause = nil
				dedup = make(map[string]bool)
			}
			if dedup[string(line)] {
				continue
			}
			dedup[string(line)] = true
			if cause == nil {
				weak = pattern.weak
			}
			cause = append(cause, line)
			break
		}
	}
	return cause
}

type buildFailureCause struct {
	pattern *regexp.Regexp
	weak    bool
}

var buildFailureCauses = [...]buildFailureCause{
	{pattern: regexp.MustCompile(`: error: `)},
	{pattern: regexp.MustCompile(`ERROR: `)},
	{pattern: regexp.MustCompile(`: fatal error: `)},
	{pattern: regexp.MustCompile(`: undefined reference to`)},
	{pattern: regexp.MustCompile(`: multiple definition of`)},
	{pattern: regexp.MustCompile(`: Permission denied`)},
	{pattern: regexp.MustCompile(`: not found`)},
	{pattern: regexp.MustCompile(`^([a-zA-Z0-9_\-/.]+):[0-9]+:([0-9]+:)?.*(error|invalid|fatal|wrong)`)},
	{pattern: regexp.MustCompile(`FAILED unresolved symbol`)},
	{pattern: regexp.MustCompile(`No rule to make target`)},
	{weak: true, pattern: regexp.MustCompile(`: final link failed: `)},
	{weak: true, pattern: regexp.MustCompile(`collect2: error: `)},
	{weak: true, pattern: regexp.MustCompile(`FAILED: Build did NOT complete`)},
}

var fileRes = []*regexp.Regexp{
	regexp.MustCompile(`^([a-zA-Z0-9_\-/.]+):[0-9]+:([0-9]+:)? `),
	regexp.MustCompile(`^(?:ld: )?(([a-zA-Z0-9_\-/.]+?)\.o):`),
	regexp.MustCompile(`; (([a-zA-Z0-9_\-/.]+?)\.o):`),
}
<|endoftext|>


<commit_before>package web

import (
	"fmt"
	"html/template"
	"net/http"
	"strconv"

	"github.com/gobuild/gobuild2/models"
	"github.com/gobuild/gobuild2/pkg/base"
	"github.com/gobuild/gobuild2/pkg/config"
	"github.com/gobuild/gobuild2/pkg/xrpc"
	"github.com/gobuild/gobuild2/routers"
	"github.com/gobuild/log"

	"github.com/codegangsta/cli"
	"github.com/go-martini/martini"
	"github.com/gobuild/middleware"
	"github.com/martini-contrib/binding"
)

func newMartini() *martini.ClassicMartini {
	r := martini.NewRouter()
	m := martini.New()
	m.Use(Logger())
	m.Use(martini.Recovery())
	// m.Use(martini.Static("public"))
	m.Use(martini.Static("public", martini.StaticOptions{SkipLogging: true}))

	m.MapTo(r, (*martini.Routes)(nil))
	m.Action(r.Handle)

	var funcMap = base.TemplateFuncs

	m.Use(middleware.ContextWithCookieSecret("", middleware.Options{
		Funcs: []template.FuncMap{funcMap},
	}))
	return &martini.ClassicMartini{m, r}
}

func Action(c *cli.Context) {
	var err error
	if err = config.Load(c.String("conf")); err != nil {
		log.Fatal(err)
	}
	if err = models.InitDB(); err != nil {
		log.Fatal(err)
	}
	cfg := config.Config
	m := newMartini()

	xrpc.HandleRpc()
	m.Get("/ruok", routers.Ruok)
	m.Any("/", routers.Home)
	m.Any("/repo", routers.Repo)
	m.Get("/**", routers.Repo)
	m.Any("/history", routers.History)
	m.Any("/download", routers.Download)
	m.Post("/new-repo", binding.Bind(routers.RepoInfoForm{}), routers.NewRepo)
	m.Any("/search", routers.Search)

	m.Group("/api", func(r martini.Router) {
		m.Get("/pkglist", routers.PkgList)
		m.Post("/build", binding.Bind(routers.RepositoryForm{}), routers.NewBuild)
		m.Post("/force-rebuild", binding.Bind(routers.TaskForm{}), routers.ForceRebuild)
	})

	// Not found handler.
	m.NotFound(routers.NotFound)

	http.Handle("/", m)

	if err = models.ResetAllTaskStatus(); err != nil {
		log.Fatalf("reset all task status: %v", err)
	}
	go drainTask()

	listenAddr := fmt.Sprintf("%s:%d",
		cfg.Server.Addr,
		cfg.Server.Port)
	log.Printf("listen %s\n", strconv.Quote(listenAddr))
	log.Fatal(http.ListenAndServe(listenAddr, nil))
}
<commit_msg>fix router order<commit_after>package web

import (
	"fmt"
	"html/template"
	"net/http"
	"strconv"

	"github.com/gobuild/gobuild2/models"
	"github.com/gobuild/gobuild2/pkg/base"
	"github.com/gobuild/gobuild2/pkg/config"
	"github.com/gobuild/gobuild2/pkg/xrpc"
	"github.com/gobuild/gobuild2/routers"
	"github.com/gobuild/log"

	"github.com/codegangsta/cli"
	"github.com/go-martini/martini"
	"github.com/gobuild/middleware"
	"github.com/martini-contrib/binding"
)

func newMartini() *martini.ClassicMartini {
	r := martini.NewRouter()
	m := martini.New()
	m.Use(Logger())
	m.Use(martini.Recovery())
	// m.Use(martini.Static("public"))
	m.Use(martini.Static("public", martini.StaticOptions{SkipLogging: true}))

	m.MapTo(r, (*martini.Routes)(nil))
	m.Action(r.Handle)

	var funcMap = base.TemplateFuncs

	m.Use(middleware.ContextWithCookieSecret("", middleware.Options{
		Funcs: []template.FuncMap{funcMap},
	}))
	return &martini.ClassicMartini{m, r}
}

func Action(c *cli.Context) {
	var err error
	if err = config.Load(c.String("conf")); err != nil {
		log.Fatal(err)
	}
	if err = models.InitDB(); err != nil {
		log.Fatal(err)
	}
	cfg := config.Config
	m := newMartini()

	xrpc.HandleRpc()
	m.Get("/ruok", routers.Ruok)
	m.Any("/", routers.Home)
	m.Any("/repo", routers.Repo)
	m.Any("/history", routers.History)
	m.Any("/download", routers.Download)
	m.Post("/new-repo", binding.Bind(routers.RepoInfoForm{}), routers.NewRepo)
	m.Any("/search", routers.Search)

	m.Group("/api", func(r martini.Router) {
		m.Get("/pkglist", routers.PkgList)
		m.Post("/build", binding.Bind(routers.RepositoryForm{}), routers.NewBuild)
		m.Post("/force-rebuild", binding.Bind(routers.TaskForm{}), routers.ForceRebuild)
	})

	m.Get("/**", routers.Repo) // for the rest of request
	// Not found handler.
	// m.NotFound(routers.NotFound)

	http.Handle("/", m)

	if err = models.ResetAllTaskStatus(); err != nil {
		log.Fatalf("reset all task status: %v", err)
	}
	go drainTask()

	listenAddr := fmt.Sprintf("%s:%d",
		cfg.Server.Addr,
		cfg.Server.Port)
	log.Printf("listen %s\n", strconv.Quote(listenAddr))
	log.Fatal(http.ListenAndServe(listenAddr, nil))
}
<|endoftext|>


<commit_before>/*
Copyright 2017 The Kubernetes Authors.

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 flags

import (
	goflag "flag"
	"fmt"
	"sort"
	"strings"
	"time"

	flag "github.com/spf13/pflag"
	v1 "k8s.io/api/core/v1"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/client-go/tools/leaderelection/resourcelock"
	"k8s.io/component-base/config"
	leaderelectionconfig "k8s.io/component-base/config/options"
)

const (
	// DefaultClusterUID is the uid to use for clusters resources created by an
	// L7 controller created without specifying the --cluster-uid flag.
	DefaultClusterUID = ""
	// DefaultNodePortRange is the list of ports or port ranges used by kubernetes for
	// allocating NodePort services.
	DefaultNodePortRange = "30000-32767"

	// DefaultLeaseDuration is a 15 second lease duration.
	DefaultLeaseDuration = 15 * time.Second
	// DefaultRenewDeadline is a 10 second renewal deadline.
	DefaultRenewDeadline = 10 * time.Second
	// DefaultRetryPeriod is a 2 second retry period.
	DefaultRetryPeriod = 2 * time.Second

	// DefaultResourceLock is the type of resource used for the lock object.
	DefaultResourceLock = resourcelock.ConfigMapsResourceLock

	// DefaultLockObjectNamespace is the namespace which owns the lock object.
	DefaultLockObjectNamespace string = "kube-system"

	// DefaultLockObjectName is the object name of the lock object.
	DefaultLockObjectName = "ingress-gce-lock"
)

var (
	// F are global flags for the controller.
	F = struct {
		APIServerHost                    string
		ASMConfigMapBasedConfigCMName    string
		ASMConfigMapBasedConfigNamespace string
		ClusterName                      string
		ConfigFilePath                   string
		DefaultSvc                       string
		DefaultSvcHealthCheckPath        string
		DefaultSvcPortName               string
		DeleteAllOnQuit                  bool
		GCEOperationPollInterval         time.Duration
		GCERateLimit                     RateLimitSpecs
		GCERateLimitScale                float64
		GKEClusterName                   string
		GKEClusterHash                   string
		GKEClusterType                   string
		HealthCheckPath                  string
		HealthzPort                      int
		InCluster                        bool
		IngressClass                     string
		KubeConfigFile                   string
		NegGCPeriod                      time.Duration
		NodePortRanges                   PortRanges
		ResyncPeriod                     time.Duration
		NumL4Workers                     int
		NumIngressWorkers                int
		RunIngressController             bool
		RunL4Controller                  bool
		L4elbRbsMode                     RbsMode
		Version                          bool
		WatchNamespace                   string
		LeaderElection                   LeaderElectionConfiguration

		// Feature flags should be named Enablexxx.
		EnableASMConfigMapBasedConfig  bool
		EnableBackendConfigHealthCheck bool
		EnableDeleteUnusedFrontends    bool
		EnableFrontendConfig           bool
		EnableNonGCPMode               bool
		EnableReadinessReflector       bool
		EnableV2FrontendNamer          bool
		FinalizerAdd                   bool // Should have been named Enablexxx.
		FinalizerRemove                bool // Should have been named Enablexxx.
		EnablePSC                      bool
		EnableIngressGAFields          bool
		EnableTrafficScaling           bool
		EnableEndpointSlices           bool
	}{
		GCERateLimitScale: 1.0,
	}
)

type LeaderElectionConfiguration struct {
	config.LeaderElectionConfiguration

	// LockObjectNamespace defines the namespace of the lock object
	LockObjectNamespace string
	// LockObjectName defines the lock object name
	LockObjectName string
}

func defaultLeaderElectionConfiguration() LeaderElectionConfiguration {
	return LeaderElectionConfiguration{
		LeaderElectionConfiguration: config.LeaderElectionConfiguration{
			LeaderElect:   true,
			LeaseDuration: metav1.Duration{Duration: DefaultLeaseDuration},
			RenewDeadline: metav1.Duration{Duration: DefaultRenewDeadline},
			RetryPeriod:   metav1.Duration{Duration: DefaultRetryPeriod},
			ResourceLock:  DefaultResourceLock,
		},

		LockObjectNamespace: DefaultLockObjectNamespace,
		LockObjectName:      DefaultLockObjectName,
	}
}

func init() {
	F.NodePortRanges.ports = []string{DefaultNodePortRange}
	F.GCERateLimit.specs = []string{"alpha.Operations.Get,qps,10,10", "beta.Operations.Get,qps,10,10", "ga.Operations.Get,qps,10,10"}
	F.LeaderElection = defaultLeaderElectionConfiguration()
}

// Register flags with the command line parser.
func Register() {
	flag.CommandLine.AddGoFlagSet(goflag.CommandLine)

	flag.StringVar(&F.APIServerHost, "apiserver-host", "",
		`The address of the Kubernetes Apiserver to connect to in the format of
protocol://address:port, e.g., http://localhost:8080. If not specified, the
assumption is that the binary runs inside a Kubernetes cluster and local
discovery is attempted.`)
	flag.StringVar(&F.ClusterName, "cluster-uid", DefaultClusterUID,
		`Optional, used to tag cluster wide, shared loadbalancer resources such
as instance groups. Use this flag if you'd like to continue using the same
resources across a pod restart. Note that this does not need to  match the name
of you Kubernetes cluster, it's just an arbitrary name used to tag/lookup cloud
resources.`)
	flag.StringVar(&F.ConfigFilePath, "config-file-path", "",
		`Path to a file containing the gce config. If left unspecified this
controller only works with default zones.`)
	flag.StringVar(&F.DefaultSvcHealthCheckPath, "default-backend-health-check-path", "/healthz",
		`Path used to health-check the default backend service. This path must serve a 200 page.
Flags default-backend-service and default-backend-service-port should never be empty - default
values will be used if not specified.`)
	flag.StringVar(&F.DefaultSvc, "default-backend-service", "kube-system/default-http-backend",
		`Service used to serve a 404 page for the default backend. Takes the
form namespace/name.`)
	flag.StringVar(&F.DefaultSvcPortName, "default-backend-service-port", "http",
		`Specify the default service's port used to serve a 404 page for the default backend. Takes
only the port's name - not its number.`)
	flag.BoolVar(&F.DeleteAllOnQuit, "delete-all-on-quit", false,
		`If true, the controller will delete all Ingress and the associated
external cloud resources as it's shutting down. Mostly used for testing. In
normal environments the controller should only delete a loadbalancer if the
associated Ingress is deleted.`)
	flag.BoolVar(&F.EnableFrontendConfig, "enable-frontend-config", false,
		`Optional, whether or not to enable FrontendConfig.`)
	flag.Var(&F.GCERateLimit, "gce-ratelimit",
		`Optional, can be used to rate limit certain GCE API calls. Example usage:
--gce-ratelimit=ga.Addresses.Get,qps,1.5,5
(limit ga.Addresses.Get to maximum of 1.5 qps with a burst of 5).
Use the flag more than once to rate limit more than one call. If you do not
specify this flag, the default is to rate limit Operations.Get for all versions.
If you do specify this flag one or more times, this default will be overwritten.
If you want to still use the default, simply specify it along with your other
values.`)
	flag.Float64Var(&F.GCERateLimitScale, "gce-ratelimit-scale", 1.0,
		`Optional, scales rate limit options by a constant factor.
1.0 is no multiplier. 5.0 means increase all rate and capacity by 5x.`)
	flag.DurationVar(&F.GCEOperationPollInterval, "gce-operation-poll-interval", time.Second,
		`Minimum time between polling requests to GCE for checking the status of an operation.`)
	flag.StringVar(&F.HealthCheckPath, "health-check-path", "/",
		`Path used to health-check a backend service. All Services must serve a
200 page on this path. Currently this is only configurable globally.`)
	flag.IntVar(&F.HealthzPort, "healthz-port", 8081,
		`Port to run healthz server. Must match the health check port in yaml.`)
	flag.BoolVar(&F.InCluster, "running-in-cluster", true,
		`Optional, if this controller is running in a kubernetes cluster, use
the pod secrets for creating a Kubernetes client.`)
	flag.StringVar(&F.KubeConfigFile, "kubeconfig", "",
		`Path to kubeconfig file with authorization and master location information.`)
	flag.DurationVar(&F.ResyncPeriod, "sync-period", 30*time.Second,
		`Relist and confirm cloud resources this often.`)
	flag.IntVar(&F.NumL4Workers, "num-l4-workers", 5,
		`Number of parallel L4 Service worker goroutines.`)
	flag.IntVar(&F.NumIngressWorkers, "num-ingress-workers", 1,
		`Number of Ingress sync-queue worker goroutines.`)
	flag.StringVar(&F.WatchNamespace, "watch-namespace", v1.NamespaceAll,
		`Namespace to watch for Ingress/Services/Endpoints.`)
	flag.BoolVar(&F.Version, "version", false,
		`Print the version of the controller and exit`)
	flag.StringVar(&F.IngressClass, "ingress-class", "",
		`If set, overrides what ingress classes are managed by the controller.`)
	flag.Var(&F.NodePortRanges, "node-port-ranges", `Node port/port-ranges whitelisted for the
L7 load balancing. CSV values accepted. Example: -node-port-ranges=80,8080,400-500`)

	leaderelectionconfig.BindLeaderElectionFlags(&F.LeaderElection.LeaderElectionConfiguration, flag.CommandLine)
	flag.StringVar(&F.LeaderElection.LockObjectNamespace, "lock-object-namespace", F.LeaderElection.LockObjectNamespace, "Define the namespace of the lock object.")
	flag.StringVar(&F.LeaderElection.LockObjectName, "lock-object-name", F.LeaderElection.LockObjectName, "Define the name of the lock object.")
	flag.DurationVar(&F.NegGCPeriod, "neg-gc-period", 120*time.Second,
		`Relist and garbage collect NEGs this often.`)
	flag.BoolVar(&F.EnableReadinessReflector, "enable-readiness-reflector", true, "Enable NEG Readiness Reflector")
	flag.BoolVar(&F.FinalizerAdd, "enable-finalizer-add",
		F.FinalizerAdd, "Enable adding Finalizer to Ingress.")
	flag.BoolVar(&F.FinalizerRemove, "enable-finalizer-remove",
		F.FinalizerRemove, "Enable removing Finalizer from Ingress.")
	flag.BoolVar(&F.EnableASMConfigMapBasedConfig, "enable-asm-config-map-config", false, "Enable ASMConfigMapBasedConfig")
	flag.StringVar(&F.ASMConfigMapBasedConfigNamespace, "asm-configmap-based-config-namespace", "kube-system", "ASM Configmap based config: configmap namespace")
	flag.StringVar(&F.ASMConfigMapBasedConfigCMName, "asm-configmap-based-config-cmname", "ingress-controller-asm-cm-config", "ASM Configmap based config: configmap name")
	flag.BoolVar(&F.EnableNonGCPMode, "enable-non-gcp-mode", false, "Set to true when running on a non-GCP cluster.")
	flag.BoolVar(&F.EnableDeleteUnusedFrontends, "enable-delete-unused-frontends", false, "Enable deleting unused gce frontend resources.")
	flag.BoolVar(&F.EnableV2FrontendNamer, "enable-v2-frontend-namer", false, "Enable v2 ingress frontend naming policy.")
	flag.BoolVar(&F.RunIngressController, "run-ingress-controller", true, `Optional, whether or not to run IngressController as part of glbc. If set to false, ingress resources will not be processed. Only the L4 Service controller will be run, if that flag is set to true.`)
	flag.BoolVar(&F.RunL4Controller, "run-l4-controller", false, `Optional, whether or not to run L4 Service Controller as part of glbc. If set to true, services of Type:LoadBalancer with Internal annotation will be processed by this controller.`)
	flag.Var(&F.L4elbRbsMode, "l4elb-rbs-mode", `If set L4NetLbController will be run.
	Possible Values:
	opt-in: manages NetLB services that contains opt-in annotation or NetLB finalizer,
	enabled: manages NetLB services that contains NetLB finalizer,
	enforced: manages all NetLB services`)
	flag.BoolVar(&F.EnableBackendConfigHealthCheck, "enable-backendconfig-healthcheck", false, "Enable configuration of HealthChecks from the BackendConfig")
	flag.BoolVar(&F.EnablePSC, "enable-psc", false, "Enable PSC controller")
	flag.BoolVar(&F.EnableIngressGAFields, "enable-ingress-ga-fields", false, "Enable using Ingress Class GA features")
	flag.StringVar(&F.GKEClusterName, "gke-cluster-name", "", "The name of the GKE cluster this Ingress Controller will be interacting with")
	flag.StringVar(&F.GKEClusterHash, "gke-cluster-hash", "", "The cluster hash of the GKE cluster this Ingress Controller will be interacting with")
	flag.StringVar(&F.GKEClusterType, "gke-cluster-type", "ZONAL", "The cluster type of the GKE cluster this Ingress Controller will be interacting with")
	flag.BoolVar(&F.EnableTrafficScaling, "enable-traffic-scaling", false, "Enable support for Service {max-rate-per-endpoint, capacity-scaler}")
	flag.BoolVar(&F.EnableEndpointSlices, "enable-endpoint-slices", false, "Enable using Endpoint Slices API instead of Endpoints API")
}

type RateLimitSpecs struct {
	specs []string
	isSet bool
}

// Part of the flag.Value interface.
func (r *RateLimitSpecs) String() string {
	return strings.Join(r.specs, ";")
}

// Set supports the flag being repeated multiple times. Part of the flag.Value interface.
func (r *RateLimitSpecs) Set(value string) error {
	// On first Set(), clear the original defaults
	// On subsequent Set()'s, append.
	if !r.isSet {
		r.specs = []string{}
		r.isSet = true
	}
	r.specs = append(r.specs, value)
	return nil
}

func (r *RateLimitSpecs) Values() []string {
	return r.specs
}

func (r *RateLimitSpecs) Type() string {
	return "rateLimitSpecs"
}

type PortRanges struct {
	ports []string
	isSet bool
}

// String is the method to format the flag's value, part of the flag.Value interface.
func (c *PortRanges) String() string {
	return strings.Join(c.ports, ",")
}

// Set supports a value of CSV or the flag repeated multiple times
func (c *PortRanges) Set(value string) error {
	// On first Set(), clear the original defaults
	if !c.isSet {
		c.isSet = true
	} else {
		return fmt.Errorf("NodePort Ranges have already been set")
	}

	c.ports = strings.Split(value, ",")
	sort.Strings(c.ports)
	return nil
}

// Set supports a value of CSV or the flag repeated multiple times
func (c *PortRanges) Values() []string {
	return c.ports
}

func (c *PortRanges) Type() string {
	return "portRanges"
}
<commit_msg>Add instance group management enabling flags<commit_after>/*
Copyright 2017 The Kubernetes Authors.

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 flags

import (
	goflag "flag"
	"fmt"
	"sort"
	"strings"
	"time"

	flag "github.com/spf13/pflag"
	v1 "k8s.io/api/core/v1"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/client-go/tools/leaderelection/resourcelock"
	"k8s.io/component-base/config"
	leaderelectionconfig "k8s.io/component-base/config/options"
)

const (
	// DefaultClusterUID is the uid to use for clusters resources created by an
	// L7 controller created without specifying the --cluster-uid flag.
	DefaultClusterUID = ""
	// DefaultNodePortRange is the list of ports or port ranges used by kubernetes for
	// allocating NodePort services.
	DefaultNodePortRange = "30000-32767"

	// DefaultLeaseDuration is a 15 second lease duration.
	DefaultLeaseDuration = 15 * time.Second
	// DefaultRenewDeadline is a 10 second renewal deadline.
	DefaultRenewDeadline = 10 * time.Second
	// DefaultRetryPeriod is a 2 second retry period.
	DefaultRetryPeriod = 2 * time.Second

	// DefaultResourceLock is the type of resource used for the lock object.
	DefaultResourceLock = resourcelock.ConfigMapsResourceLock

	// DefaultLockObjectNamespace is the namespace which owns the lock object.
	DefaultLockObjectNamespace string = "kube-system"

	// DefaultLockObjectName is the object name of the lock object.
	DefaultLockObjectName = "ingress-gce-lock"
)

var (
	// F are global flags for the controller.
	F = struct {
		APIServerHost                    string
		ASMConfigMapBasedConfigCMName    string
		ASMConfigMapBasedConfigNamespace string
		ClusterName                      string
		ConfigFilePath                   string
		DefaultSvc                       string
		DefaultSvcHealthCheckPath        string
		DefaultSvcPortName               string
		DeleteAllOnQuit                  bool
		GCEOperationPollInterval         time.Duration
		GCERateLimit                     RateLimitSpecs
		GCERateLimitScale                float64
		GKEClusterName                   string
		GKEClusterHash                   string
		GKEClusterType                   string
		HealthCheckPath                  string
		HealthzPort                      int
		InCluster                        bool
		IngressClass                     string
		KubeConfigFile                   string
		NegGCPeriod                      time.Duration
		NodePortRanges                   PortRanges
		ResyncPeriod                     time.Duration
		NumL4Workers                     int
		NumIngressWorkers                int
		RunIngressController             bool
		RunL4Controller                  bool
		L4elbRbsMode                     RbsMode
		Version                          bool
		WatchNamespace                   string
		LeaderElection                   LeaderElectionConfiguration

		// Feature flags should be named Enablexxx.
		EnableASMConfigMapBasedConfig  bool
		EnableBackendConfigHealthCheck bool
		EnableDeleteUnusedFrontends    bool
		EnableFrontendConfig           bool
		EnableNonGCPMode               bool
		EnableReadinessReflector       bool
		EnableV2FrontendNamer          bool
		FinalizerAdd                   bool // Should have been named Enablexxx.
		FinalizerRemove                bool // Should have been named Enablexxx.
		EnablePSC                      bool
		EnableIngressGAFields          bool
		EnableTrafficScaling           bool
		EnableEndpointSlices           bool
		EnableMultipleIgs              bool
		MaxIgSize                      int
	}{
		GCERateLimitScale: 1.0,
	}
)

type LeaderElectionConfiguration struct {
	config.LeaderElectionConfiguration

	// LockObjectNamespace defines the namespace of the lock object
	LockObjectNamespace string
	// LockObjectName defines the lock object name
	LockObjectName string
}

func defaultLeaderElectionConfiguration() LeaderElectionConfiguration {
	return LeaderElectionConfiguration{
		LeaderElectionConfiguration: config.LeaderElectionConfiguration{
			LeaderElect:   true,
			LeaseDuration: metav1.Duration{Duration: DefaultLeaseDuration},
			RenewDeadline: metav1.Duration{Duration: DefaultRenewDeadline},
			RetryPeriod:   metav1.Duration{Duration: DefaultRetryPeriod},
			ResourceLock:  DefaultResourceLock,
		},

		LockObjectNamespace: DefaultLockObjectNamespace,
		LockObjectName:      DefaultLockObjectName,
	}
}

func init() {
	F.NodePortRanges.ports = []string{DefaultNodePortRange}
	F.GCERateLimit.specs = []string{"alpha.Operations.Get,qps,10,10", "beta.Operations.Get,qps,10,10", "ga.Operations.Get,qps,10,10"}
	F.LeaderElection = defaultLeaderElectionConfiguration()
}

// Register flags with the command line parser.
func Register() {
	flag.CommandLine.AddGoFlagSet(goflag.CommandLine)

	flag.StringVar(&F.APIServerHost, "apiserver-host", "",
		`The address of the Kubernetes Apiserver to connect to in the format of
protocol://address:port, e.g., http://localhost:8080. If not specified, the
assumption is that the binary runs inside a Kubernetes cluster and local
discovery is attempted.`)
	flag.StringVar(&F.ClusterName, "cluster-uid", DefaultClusterUID,
		`Optional, used to tag cluster wide, shared loadbalancer resources such
as instance groups. Use this flag if you'd like to continue using the same
resources across a pod restart. Note that this does not need to  match the name
of you Kubernetes cluster, it's just an arbitrary name used to tag/lookup cloud
resources.`)
	flag.StringVar(&F.ConfigFilePath, "config-file-path", "",
		`Path to a file containing the gce config. If left unspecified this
controller only works with default zones.`)
	flag.StringVar(&F.DefaultSvcHealthCheckPath, "default-backend-health-check-path", "/healthz",
		`Path used to health-check the default backend service. This path must serve a 200 page.
Flags default-backend-service and default-backend-service-port should never be empty - default
values will be used if not specified.`)
	flag.StringVar(&F.DefaultSvc, "default-backend-service", "kube-system/default-http-backend",
		`Service used to serve a 404 page for the default backend. Takes the
form namespace/name.`)
	flag.StringVar(&F.DefaultSvcPortName, "default-backend-service-port", "http",
		`Specify the default service's port used to serve a 404 page for the default backend. Takes
only the port's name - not its number.`)
	flag.BoolVar(&F.DeleteAllOnQuit, "delete-all-on-quit", false,
		`If true, the controller will delete all Ingress and the associated
external cloud resources as it's shutting down. Mostly used for testing. In
normal environments the controller should only delete a loadbalancer if the
associated Ingress is deleted.`)
	flag.BoolVar(&F.EnableFrontendConfig, "enable-frontend-config", false,
		`Optional, whether or not to enable FrontendConfig.`)
	flag.Var(&F.GCERateLimit, "gce-ratelimit",
		`Optional, can be used to rate limit certain GCE API calls. Example usage:
--gce-ratelimit=ga.Addresses.Get,qps,1.5,5
(limit ga.Addresses.Get to maximum of 1.5 qps with a burst of 5).
Use the flag more than once to rate limit more than one call. If you do not
specify this flag, the default is to rate limit Operations.Get for all versions.
If you do specify this flag one or more times, this default will be overwritten.
If you want to still use the default, simply specify it along with your other
values.`)
	flag.Float64Var(&F.GCERateLimitScale, "gce-ratelimit-scale", 1.0,
		`Optional, scales rate limit options by a constant factor.
1.0 is no multiplier. 5.0 means increase all rate and capacity by 5x.`)
	flag.DurationVar(&F.GCEOperationPollInterval, "gce-operation-poll-interval", time.Second,
		`Minimum time between polling requests to GCE for checking the status of an operation.`)
	flag.StringVar(&F.HealthCheckPath, "health-check-path", "/",
		`Path used to health-check a backend service. All Services must serve a
200 page on this path. Currently this is only configurable globally.`)
	flag.IntVar(&F.HealthzPort, "healthz-port", 8081,
		`Port to run healthz server. Must match the health check port in yaml.`)
	flag.BoolVar(&F.InCluster, "running-in-cluster", true,
		`Optional, if this controller is running in a kubernetes cluster, use
the pod secrets for creating a Kubernetes client.`)
	flag.StringVar(&F.KubeConfigFile, "kubeconfig", "",
		`Path to kubeconfig file with authorization and master location information.`)
	flag.DurationVar(&F.ResyncPeriod, "sync-period", 30*time.Second,
		`Relist and confirm cloud resources this often.`)
	flag.IntVar(&F.NumL4Workers, "num-l4-workers", 5,
		`Number of parallel L4 Service worker goroutines.`)
	flag.IntVar(&F.NumIngressWorkers, "num-ingress-workers", 1,
		`Number of Ingress sync-queue worker goroutines.`)
	flag.StringVar(&F.WatchNamespace, "watch-namespace", v1.NamespaceAll,
		`Namespace to watch for Ingress/Services/Endpoints.`)
	flag.BoolVar(&F.Version, "version", false,
		`Print the version of the controller and exit`)
	flag.StringVar(&F.IngressClass, "ingress-class", "",
		`If set, overrides what ingress classes are managed by the controller.`)
	flag.Var(&F.NodePortRanges, "node-port-ranges", `Node port/port-ranges whitelisted for the
L7 load balancing. CSV values accepted. Example: -node-port-ranges=80,8080,400-500`)

	leaderelectionconfig.BindLeaderElectionFlags(&F.LeaderElection.LeaderElectionConfiguration, flag.CommandLine)
	flag.StringVar(&F.LeaderElection.LockObjectNamespace, "lock-object-namespace", F.LeaderElection.LockObjectNamespace, "Define the namespace of the lock object.")
	flag.StringVar(&F.LeaderElection.LockObjectName, "lock-object-name", F.LeaderElection.LockObjectName, "Define the name of the lock object.")
	flag.DurationVar(&F.NegGCPeriod, "neg-gc-period", 120*time.Second,
		`Relist and garbage collect NEGs this often.`)
	flag.BoolVar(&F.EnableReadinessReflector, "enable-readiness-reflector", true, "Enable NEG Readiness Reflector")
	flag.BoolVar(&F.FinalizerAdd, "enable-finalizer-add",
		F.FinalizerAdd, "Enable adding Finalizer to Ingress.")
	flag.BoolVar(&F.FinalizerRemove, "enable-finalizer-remove",
		F.FinalizerRemove, "Enable removing Finalizer from Ingress.")
	flag.BoolVar(&F.EnableASMConfigMapBasedConfig, "enable-asm-config-map-config", false, "Enable ASMConfigMapBasedConfig")
	flag.StringVar(&F.ASMConfigMapBasedConfigNamespace, "asm-configmap-based-config-namespace", "kube-system", "ASM Configmap based config: configmap namespace")
	flag.StringVar(&F.ASMConfigMapBasedConfigCMName, "asm-configmap-based-config-cmname", "ingress-controller-asm-cm-config", "ASM Configmap based config: configmap name")
	flag.BoolVar(&F.EnableNonGCPMode, "enable-non-gcp-mode", false, "Set to true when running on a non-GCP cluster.")
	flag.BoolVar(&F.EnableDeleteUnusedFrontends, "enable-delete-unused-frontends", false, "Enable deleting unused gce frontend resources.")
	flag.BoolVar(&F.EnableV2FrontendNamer, "enable-v2-frontend-namer", false, "Enable v2 ingress frontend naming policy.")
	flag.BoolVar(&F.RunIngressController, "run-ingress-controller", true, `Optional, whether or not to run IngressController as part of glbc. If set to false, ingress resources will not be processed. Only the L4 Service controller will be run, if that flag is set to true.`)
	flag.BoolVar(&F.RunL4Controller, "run-l4-controller", false, `Optional, whether or not to run L4 Service Controller as part of glbc. If set to true, services of Type:LoadBalancer with Internal annotation will be processed by this controller.`)
	flag.Var(&F.L4elbRbsMode, "l4elb-rbs-mode", `If set L4NetLbController will be run.
	Possible Values:
	opt-in: manages NetLB services that contains opt-in annotation or NetLB finalizer,
	enabled: manages NetLB services that contains NetLB finalizer,
	enforced: manages all NetLB services`)
	flag.BoolVar(&F.EnableBackendConfigHealthCheck, "enable-backendconfig-healthcheck", false, "Enable configuration of HealthChecks from the BackendConfig")
	flag.BoolVar(&F.EnablePSC, "enable-psc", false, "Enable PSC controller")
	flag.BoolVar(&F.EnableIngressGAFields, "enable-ingress-ga-fields", false, "Enable using Ingress Class GA features")
	flag.StringVar(&F.GKEClusterName, "gke-cluster-name", "", "The name of the GKE cluster this Ingress Controller will be interacting with")
	flag.StringVar(&F.GKEClusterHash, "gke-cluster-hash", "", "The cluster hash of the GKE cluster this Ingress Controller will be interacting with")
	flag.StringVar(&F.GKEClusterType, "gke-cluster-type", "ZONAL", "The cluster type of the GKE cluster this Ingress Controller will be interacting with")
	flag.BoolVar(&F.EnableTrafficScaling, "enable-traffic-scaling", false, "Enable support for Service {max-rate-per-endpoint, capacity-scaler}")
	flag.BoolVar(&F.EnableEndpointSlices, "enable-endpoint-slices", false, "Enable using Endpoint Slices API instead of Endpoints API")
	flag.BoolVar(&F.EnableMultipleIgs, "enable-multiple-igs", false, "Enable using unmanaged instance group management")
	flag.IntVar(&F.MaxIgSize, "max-ig-size", 1000, "Max number of instances in Instance Group")
}

type RateLimitSpecs struct {
	specs []string
	isSet bool
}

// Part of the flag.Value interface.
func (r *RateLimitSpecs) String() string {
	return strings.Join(r.specs, ";")
}

// Set supports the flag being repeated multiple times. Part of the flag.Value interface.
func (r *RateLimitSpecs) Set(value string) error {
	// On first Set(), clear the original defaults
	// On subsequent Set()'s, append.
	if !r.isSet {
		r.specs = []string{}
		r.isSet = true
	}
	r.specs = append(r.specs, value)
	return nil
}

func (r *RateLimitSpecs) Values() []string {
	return r.specs
}

func (r *RateLimitSpecs) Type() string {
	return "rateLimitSpecs"
}

type PortRanges struct {
	ports []string
	isSet bool
}

// String is the method to format the flag's value, part of the flag.Value interface.
func (c *PortRanges) String() string {
	return strings.Join(c.ports, ",")
}

// Set supports a value of CSV or the flag repeated multiple times
func (c *PortRanges) Set(value string) error {
	// On first Set(), clear the original defaults
	if !c.isSet {
		c.isSet = true
	} else {
		return fmt.Errorf("NodePort Ranges have already been set")
	}

	c.ports = strings.Split(value, ",")
	sort.Strings(c.ports)
	return nil
}

// Set supports a value of CSV or the flag repeated multiple times
func (c *PortRanges) Values() []string {
	return c.ports
}

func (c *PortRanges) Type() string {
	return "portRanges"
}
<|endoftext|>


<commit_before>/*
Copyright The Helm Authors.

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 helm

import (
	"crypto/tls"
	"time"

	"github.com/golang/protobuf/proto"
	"golang.org/x/net/context"
	"google.golang.org/grpc/metadata"

	cpb "k8s.io/helm/pkg/proto/hapi/chart"
	"k8s.io/helm/pkg/proto/hapi/release"
	rls "k8s.io/helm/pkg/proto/hapi/services"
	"k8s.io/helm/pkg/version"
)

// Option allows specifying various settings configurable by
// the helm client user for overriding the defaults used when
// issuing rpc's to the Tiller release server.
type Option func(*options)

// options specify optional settings used by the helm client.
type options struct {
	// value of helm home override
	host string
	// if set dry-run helm client calls
	dryRun bool
	// if set enable TLS on helm client calls
	useTLS bool
	// if set, re-use an existing name
	reuseName bool
	// if set, performs pod restart during upgrade/rollback
	recreate bool
	// if set, force resource update through delete/recreate if needed
	force bool
	// if set, skip running hooks
	disableHooks bool
	// if set, skip CRD hook only
	disableCRDHook bool
	// if set, render SubChart Notes
	releaseName string
	// tls.Config to use for rpc if tls enabled
	tlsConfig *tls.Config
	// release list options are applied directly to the list releases request
	listReq rls.ListReleasesRequest
	// release install options are applied directly to the install release request
	instReq rls.InstallReleaseRequest
	// release update options are applied directly to the update release request
	updateReq rls.UpdateReleaseRequest
	// release uninstall options are applied directly to the uninstall release request
	uninstallReq rls.UninstallReleaseRequest
	// release get status options are applied directly to the get release status request
	statusReq rls.GetReleaseStatusRequest
	// release get content options are applied directly to the get release content request
	contentReq rls.GetReleaseContentRequest
	// release rollback options are applied directly to the rollback release request
	rollbackReq rls.RollbackReleaseRequest
	// before intercepts client calls before sending
	before func(context.Context, proto.Message) error
	// release history options are applied directly to the get release history request
	histReq rls.GetHistoryRequest
	// resetValues instructs Tiller to reset values to their defaults.
	resetValues bool
	// reuseValues instructs Tiller to reuse the values from the last release.
	reuseValues bool
	// release test options are applied directly to the test release history request
	testReq rls.TestReleaseRequest
	// connectTimeout specifies the time duration Helm will wait to establish a connection to tiller
	connectTimeout time.Duration
}

// Host specifies the host address of the Tiller release server, (default = ":44134").
func Host(host string) Option {
	return func(opts *options) {
		opts.host = host
	}
}

// WithTLS specifies the tls configuration if the helm client is enabled to use TLS.
func WithTLS(cfg *tls.Config) Option {
	return func(opts *options) {
		opts.useTLS = true
		opts.tlsConfig = cfg
	}
}

// BeforeCall returns an option that allows intercepting a helm client rpc
// before being sent OTA to tiller. The intercepting function should return
// an error to indicate that the call should not proceed or nil otherwise.
func BeforeCall(fn func(context.Context, proto.Message) error) Option {
	return func(opts *options) {
		opts.before = fn
	}
}

// ReleaseListOption allows specifying various settings
// configurable by the helm client user for overriding
// the defaults used when running the `helm list` command.
type ReleaseListOption func(*options)

// ReleaseListOffset specifies the offset into a list of releases.
func ReleaseListOffset(offset string) ReleaseListOption {
	return func(opts *options) {
		opts.listReq.Offset = offset
	}
}

// ReleaseListFilter specifies a filter to apply a list of releases.
func ReleaseListFilter(filter string) ReleaseListOption {
	return func(opts *options) {
		opts.listReq.Filter = filter
	}
}

// ReleaseListLimit set an upper bound on the number of releases returned.
func ReleaseListLimit(limit int) ReleaseListOption {
	return func(opts *options) {
		opts.listReq.Limit = int64(limit)
	}
}

// ReleaseListOrder specifies how to order a list of releases.
func ReleaseListOrder(order int32) ReleaseListOption {
	return func(opts *options) {
		opts.listReq.SortOrder = rls.ListSort_SortOrder(order)
	}
}

// ReleaseListSort specifies how to sort a release list.
func ReleaseListSort(sort int32) ReleaseListOption {
	return func(opts *options) {
		opts.listReq.SortBy = rls.ListSort_SortBy(sort)
	}
}

// ReleaseListStatuses specifies which status codes should be returned.
func ReleaseListStatuses(statuses []release.Status_Code) ReleaseListOption {
	return func(opts *options) {
		if len(statuses) == 0 {
			statuses = []release.Status_Code{release.Status_DEPLOYED}
		}
		opts.listReq.StatusCodes = statuses
	}
}

// ReleaseListNamespace specifies the namespace to list releases from
func ReleaseListNamespace(namespace string) ReleaseListOption {
	return func(opts *options) {
		opts.listReq.Namespace = namespace
	}
}

// InstallOption allows specifying various settings
// configurable by the helm client user for overriding
// the defaults used when running the `helm install` command.
type InstallOption func(*options)

// ValueOverrides specifies a list of values to include when installing.
func ValueOverrides(raw []byte) InstallOption {
	return func(opts *options) {
		opts.instReq.Values = &cpb.Config{Raw: string(raw)}
	}
}

// ReleaseName specifies the name of the release when installing.
func ReleaseName(name string) InstallOption {
	return func(opts *options) {
		opts.instReq.Name = name
	}
}

// ConnectTimeout specifies the duration (in seconds) Helm will wait to establish a connection to tiller
func ConnectTimeout(timeout int64) Option {
	return func(opts *options) {
		opts.connectTimeout = time.Duration(timeout) * time.Second
	}
}

// InstallTimeout specifies the number of seconds before kubernetes calls timeout
func InstallTimeout(timeout int64) InstallOption {
	return func(opts *options) {
		opts.instReq.Timeout = timeout
	}
}

// UpgradeTimeout specifies the number of seconds before kubernetes calls timeout
func UpgradeTimeout(timeout int64) UpdateOption {
	return func(opts *options) {
		opts.updateReq.Timeout = timeout
	}
}

// DeleteTimeout specifies the number of seconds before kubernetes calls timeout
func DeleteTimeout(timeout int64) DeleteOption {
	return func(opts *options) {
		opts.uninstallReq.Timeout = timeout
	}
}

// ReleaseTestTimeout specifies the number of seconds before kubernetes calls timeout
func ReleaseTestTimeout(timeout int64) ReleaseTestOption {
	return func(opts *options) {
		opts.testReq.Timeout = timeout
	}
}

// ReleaseTestCleanup is a boolean value representing whether to cleanup test pods
func ReleaseTestCleanup(cleanup bool) ReleaseTestOption {
	return func(opts *options) {
		opts.testReq.Cleanup = cleanup
	}
}

// RollbackTimeout specifies the number of seconds before kubernetes calls timeout
func RollbackTimeout(timeout int64) RollbackOption {
	return func(opts *options) {
		opts.rollbackReq.Timeout = timeout
	}
}

// InstallWait specifies whether or not to wait for all resources to be ready
func InstallWait(wait bool) InstallOption {
	return func(opts *options) {
		opts.instReq.Wait = wait
	}
}

// UpgradeWait specifies whether or not to wait for all resources to be ready
func UpgradeWait(wait bool) UpdateOption {
	return func(opts *options) {
		opts.updateReq.Wait = wait
	}
}

// RollbackWait specifies whether or not to wait for all resources to be ready
func RollbackWait(wait bool) RollbackOption {
	return func(opts *options) {
		opts.rollbackReq.Wait = wait
	}
}

// UpdateValueOverrides specifies a list of values to include when upgrading
func UpdateValueOverrides(raw []byte) UpdateOption {
	return func(opts *options) {
		opts.updateReq.Values = &cpb.Config{Raw: string(raw)}
	}
}

// InstallDescription specifies the description for the release
func InstallDescription(description string) InstallOption {
	return func(opts *options) {
		opts.instReq.Description = description
	}
}

// UpgradeDescription specifies the description for the update
func UpgradeDescription(description string) UpdateOption {
	return func(opts *options) {
		opts.updateReq.Description = description
	}
}

// RollbackDescription specifies the description for the release
func RollbackDescription(description string) RollbackOption {
	return func(opts *options) {
		opts.rollbackReq.Description = description
	}
}

// DeleteDescription specifies the description for the release
func DeleteDescription(description string) DeleteOption {
	return func(opts *options) {
		opts.uninstallReq.Description = description
	}
}

// DeleteDisableHooks will disable hooks for a deletion operation.
func DeleteDisableHooks(disable bool) DeleteOption {
	return func(opts *options) {
		opts.disableHooks = disable
	}
}

// DeleteDryRun will (if true) execute a deletion as a dry run.
func DeleteDryRun(dry bool) DeleteOption {
	return func(opts *options) {
		opts.dryRun = dry
	}
}

// DeletePurge removes the release from the store and make its name free for later use.
func DeletePurge(purge bool) DeleteOption {
	return func(opts *options) {
		opts.uninstallReq.Purge = purge
	}
}

// InstallDryRun will (if true) execute an installation as a dry run.
func InstallDryRun(dry bool) InstallOption {
	return func(opts *options) {
		opts.dryRun = dry
	}
}

// InstallDisableHooks disables hooks during installation.
func InstallDisableHooks(disable bool) InstallOption {
	return func(opts *options) {
		opts.disableHooks = disable
	}
}

// InstallDisableCRDHook disables CRD hook during installation.
func InstallDisableCRDHook(disable bool) InstallOption {
	return func(opts *options) {
		opts.disableCRDHook = disable
	}
}

// InstallReuseName will (if true) instruct Tiller to re-use an existing name.
func InstallReuseName(reuse bool) InstallOption {
	return func(opts *options) {
		opts.reuseName = reuse
	}
}

// InstallSubNotes will (if true) instruct Tiller to render SubChart Notes
func InstallSubNotes(enable bool) InstallOption {
	return func(opts *options) {
		opts.instReq.SubNotes = enable
	}
}

// UpgradeSubNotes will (if true) instruct Tiller to render SubChart Notes
func UpgradeSubNotes(enable bool) UpdateOption {
	return func(opts *options) {
		opts.updateReq.SubNotes = enable
	}
}

// RollbackDisableHooks will disable hooks for a rollback operation
func RollbackDisableHooks(disable bool) RollbackOption {
	return func(opts *options) {
		opts.disableHooks = disable
	}
}

// RollbackDryRun will (if true) execute a rollback as a dry run.
func RollbackDryRun(dry bool) RollbackOption {
	return func(opts *options) {
		opts.dryRun = dry
	}
}

// RollbackRecreate will (if true) recreate pods after rollback.
func RollbackRecreate(recreate bool) RollbackOption {
	return func(opts *options) {
		opts.recreate = recreate
	}
}

// RollbackForce will (if true) force resource update through delete/recreate if needed
func RollbackForce(force bool) RollbackOption {
	return func(opts *options) {
		opts.force = force
	}
}

// RollbackVersion sets the version of the release to deploy.
func RollbackVersion(ver int32) RollbackOption {
	return func(opts *options) {
		opts.rollbackReq.Version = ver
	}
}

// UpgradeDisableHooks will disable hooks for an upgrade operation.
func UpgradeDisableHooks(disable bool) UpdateOption {
	return func(opts *options) {
		opts.disableHooks = disable
	}
}

// UpgradeDryRun will (if true) execute an upgrade as a dry run.
func UpgradeDryRun(dry bool) UpdateOption {
	return func(opts *options) {
		opts.dryRun = dry
	}
}

// ResetValues will (if true) trigger resetting the values to their original state.
func ResetValues(reset bool) UpdateOption {
	return func(opts *options) {
		opts.resetValues = reset
	}
}

// ReuseValues will cause Tiller to reuse the values from the last release.
// This is ignored if ResetValues is true.
func ReuseValues(reuse bool) UpdateOption {
	return func(opts *options) {
		opts.reuseValues = reuse
	}
}

// UpgradeRecreate will (if true) recreate pods after upgrade.
func UpgradeRecreate(recreate bool) UpdateOption {
	return func(opts *options) {
		opts.recreate = recreate
	}
}

// UpgradeForce will (if true) force resource update through delete/recreate if needed
func UpgradeForce(force bool) UpdateOption {
	return func(opts *options) {
		opts.force = force
	}
}

// ContentOption allows setting optional attributes when
// performing a GetReleaseContent tiller rpc.
type ContentOption func(*options)

// ContentReleaseVersion will instruct Tiller to retrieve the content
// of a particular version of a release.
func ContentReleaseVersion(version int32) ContentOption {
	return func(opts *options) {
		opts.contentReq.Version = version
	}
}

// StatusOption allows setting optional attributes when
// performing a GetReleaseStatus tiller rpc.
type StatusOption func(*options)

// StatusReleaseVersion will instruct Tiller to retrieve the status
// of a particular version of a release.
func StatusReleaseVersion(version int32) StatusOption {
	return func(opts *options) {
		opts.statusReq.Version = version
	}
}

// DeleteOption allows setting optional attributes when
// performing a UninstallRelease tiller rpc.
type DeleteOption func(*options)

// VersionOption -- TODO
type VersionOption func(*options)

// UpdateOption allows specifying various settings
// configurable by the helm client user for overriding
// the defaults used when running the `helm upgrade` command.
type UpdateOption func(*options)

// RollbackOption allows specififying various settings configurable
// by the helm client user for overriding the defaults used when
// running the `helm rollback` command.
type RollbackOption func(*options)

// HistoryOption allows configuring optional request data for
// issuing a GetHistory rpc.
type HistoryOption func(*options)

// WithMaxHistory sets the max number of releases to return
// in a release history query.
func WithMaxHistory(max int32) HistoryOption {
	return func(opts *options) {
		opts.histReq.Max = max
	}
}

// NewContext creates a versioned context.
func NewContext() context.Context {
	md := metadata.Pairs("x-helm-api-client", version.GetVersion())
	return metadata.NewOutgoingContext(context.TODO(), md)
}

// ReleaseTestOption allows configuring optional request data for
// issuing a TestRelease rpc.
type ReleaseTestOption func(*options)
<commit_msg>Restore comment text<commit_after>/*
Copyright The Helm Authors.

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 helm

import (
	"crypto/tls"
	"time"

	"github.com/golang/protobuf/proto"
	"golang.org/x/net/context"
	"google.golang.org/grpc/metadata"

	cpb "k8s.io/helm/pkg/proto/hapi/chart"
	"k8s.io/helm/pkg/proto/hapi/release"
	rls "k8s.io/helm/pkg/proto/hapi/services"
	"k8s.io/helm/pkg/version"
)

// Option allows specifying various settings configurable by
// the helm client user for overriding the defaults used when
// issuing rpc's to the Tiller release server.
type Option func(*options)

// options specify optional settings used by the helm client.
type options struct {
	// value of helm home override
	host string
	// if set dry-run helm client calls
	dryRun bool
	// if set enable TLS on helm client calls
	useTLS bool
	// if set, re-use an existing name
	reuseName bool
	// if set, performs pod restart during upgrade/rollback
	recreate bool
	// if set, force resource update through delete/recreate if needed
	force bool
	// if set, skip running hooks
	disableHooks bool
	// if set, skip CRD hook only
	disableCRDHook bool
	// name of release
	releaseName string
	// tls.Config to use for rpc if tls enabled
	tlsConfig *tls.Config
	// release list options are applied directly to the list releases request
	listReq rls.ListReleasesRequest
	// release install options are applied directly to the install release request
	instReq rls.InstallReleaseRequest
	// release update options are applied directly to the update release request
	updateReq rls.UpdateReleaseRequest
	// release uninstall options are applied directly to the uninstall release request
	uninstallReq rls.UninstallReleaseRequest
	// release get status options are applied directly to the get release status request
	statusReq rls.GetReleaseStatusRequest
	// release get content options are applied directly to the get release content request
	contentReq rls.GetReleaseContentRequest
	// release rollback options are applied directly to the rollback release request
	rollbackReq rls.RollbackReleaseRequest
	// before intercepts client calls before sending
	before func(context.Context, proto.Message) error
	// release history options are applied directly to the get release history request
	histReq rls.GetHistoryRequest
	// resetValues instructs Tiller to reset values to their defaults.
	resetValues bool
	// reuseValues instructs Tiller to reuse the values from the last release.
	reuseValues bool
	// release test options are applied directly to the test release history request
	testReq rls.TestReleaseRequest
	// connectTimeout specifies the time duration Helm will wait to establish a connection to tiller
	connectTimeout time.Duration
}

// Host specifies the host address of the Tiller release server, (default = ":44134").
func Host(host string) Option {
	return func(opts *options) {
		opts.host = host
	}
}

// WithTLS specifies the tls configuration if the helm client is enabled to use TLS.
func WithTLS(cfg *tls.Config) Option {
	return func(opts *options) {
		opts.useTLS = true
		opts.tlsConfig = cfg
	}
}

// BeforeCall returns an option that allows intercepting a helm client rpc
// before being sent OTA to tiller. The intercepting function should return
// an error to indicate that the call should not proceed or nil otherwise.
func BeforeCall(fn func(context.Context, proto.Message) error) Option {
	return func(opts *options) {
		opts.before = fn
	}
}

// ReleaseListOption allows specifying various settings
// configurable by the helm client user for overriding
// the defaults used when running the `helm list` command.
type ReleaseListOption func(*options)

// ReleaseListOffset specifies the offset into a list of releases.
func ReleaseListOffset(offset string) ReleaseListOption {
	return func(opts *options) {
		opts.listReq.Offset = offset
	}
}

// ReleaseListFilter specifies a filter to apply a list of releases.
func ReleaseListFilter(filter string) ReleaseListOption {
	return func(opts *options) {
		opts.listReq.Filter = filter
	}
}

// ReleaseListLimit set an upper bound on the number of releases returned.
func ReleaseListLimit(limit int) ReleaseListOption {
	return func(opts *options) {
		opts.listReq.Limit = int64(limit)
	}
}

// ReleaseListOrder specifies how to order a list of releases.
func ReleaseListOrder(order int32) ReleaseListOption {
	return func(opts *options) {
		opts.listReq.SortOrder = rls.ListSort_SortOrder(order)
	}
}

// ReleaseListSort specifies how to sort a release list.
func ReleaseListSort(sort int32) ReleaseListOption {
	return func(opts *options) {
		opts.listReq.SortBy = rls.ListSort_SortBy(sort)
	}
}

// ReleaseListStatuses specifies which status codes should be returned.
func ReleaseListStatuses(statuses []release.Status_Code) ReleaseListOption {
	return func(opts *options) {
		if len(statuses) == 0 {
			statuses = []release.Status_Code{release.Status_DEPLOYED}
		}
		opts.listReq.StatusCodes = statuses
	}
}

// ReleaseListNamespace specifies the namespace to list releases from
func ReleaseListNamespace(namespace string) ReleaseListOption {
	return func(opts *options) {
		opts.listReq.Namespace = namespace
	}
}

// InstallOption allows specifying various settings
// configurable by the helm client user for overriding
// the defaults used when running the `helm install` command.
type InstallOption func(*options)

// ValueOverrides specifies a list of values to include when installing.
func ValueOverrides(raw []byte) InstallOption {
	return func(opts *options) {
		opts.instReq.Values = &cpb.Config{Raw: string(raw)}
	}
}

// ReleaseName specifies the name of the release when installing.
func ReleaseName(name string) InstallOption {
	return func(opts *options) {
		opts.instReq.Name = name
	}
}

// ConnectTimeout specifies the duration (in seconds) Helm will wait to establish a connection to tiller
func ConnectTimeout(timeout int64) Option {
	return func(opts *options) {
		opts.connectTimeout = time.Duration(timeout) * time.Second
	}
}

// InstallTimeout specifies the number of seconds before kubernetes calls timeout
func InstallTimeout(timeout int64) InstallOption {
	return func(opts *options) {
		opts.instReq.Timeout = timeout
	}
}

// UpgradeTimeout specifies the number of seconds before kubernetes calls timeout
func UpgradeTimeout(timeout int64) UpdateOption {
	return func(opts *options) {
		opts.updateReq.Timeout = timeout
	}
}

// DeleteTimeout specifies the number of seconds before kubernetes calls timeout
func DeleteTimeout(timeout int64) DeleteOption {
	return func(opts *options) {
		opts.uninstallReq.Timeout = timeout
	}
}

// ReleaseTestTimeout specifies the number of seconds before kubernetes calls timeout
func ReleaseTestTimeout(timeout int64) ReleaseTestOption {
	return func(opts *options) {
		opts.testReq.Timeout = timeout
	}
}

// ReleaseTestCleanup is a boolean value representing whether to cleanup test pods
func ReleaseTestCleanup(cleanup bool) ReleaseTestOption {
	return func(opts *options) {
		opts.testReq.Cleanup = cleanup
	}
}

// RollbackTimeout specifies the number of seconds before kubernetes calls timeout
func RollbackTimeout(timeout int64) RollbackOption {
	return func(opts *options) {
		opts.rollbackReq.Timeout = timeout
	}
}

// InstallWait specifies whether or not to wait for all resources to be ready
func InstallWait(wait bool) InstallOption {
	return func(opts *options) {
		opts.instReq.Wait = wait
	}
}

// UpgradeWait specifies whether or not to wait for all resources to be ready
func UpgradeWait(wait bool) UpdateOption {
	return func(opts *options) {
		opts.updateReq.Wait = wait
	}
}

// RollbackWait specifies whether or not to wait for all resources to be ready
func RollbackWait(wait bool) RollbackOption {
	return func(opts *options) {
		opts.rollbackReq.Wait = wait
	}
}

// UpdateValueOverrides specifies a list of values to include when upgrading
func UpdateValueOverrides(raw []byte) UpdateOption {
	return func(opts *options) {
		opts.updateReq.Values = &cpb.Config{Raw: string(raw)}
	}
}

// InstallDescription specifies the description for the release
func InstallDescription(description string) InstallOption {
	return func(opts *options) {
		opts.instReq.Description = description
	}
}

// UpgradeDescription specifies the description for the update
func UpgradeDescription(description string) UpdateOption {
	return func(opts *options) {
		opts.updateReq.Description = description
	}
}

// RollbackDescription specifies the description for the release
func RollbackDescription(description string) RollbackOption {
	return func(opts *options) {
		opts.rollbackReq.Description = description
	}
}

// DeleteDescription specifies the description for the release
func DeleteDescription(description string) DeleteOption {
	return func(opts *options) {
		opts.uninstallReq.Description = description
	}
}

// DeleteDisableHooks will disable hooks for a deletion operation.
func DeleteDisableHooks(disable bool) DeleteOption {
	return func(opts *options) {
		opts.disableHooks = disable
	}
}

// DeleteDryRun will (if true) execute a deletion as a dry run.
func DeleteDryRun(dry bool) DeleteOption {
	return func(opts *options) {
		opts.dryRun = dry
	}
}

// DeletePurge removes the release from the store and make its name free for later use.
func DeletePurge(purge bool) DeleteOption {
	return func(opts *options) {
		opts.uninstallReq.Purge = purge
	}
}

// InstallDryRun will (if true) execute an installation as a dry run.
func InstallDryRun(dry bool) InstallOption {
	return func(opts *options) {
		opts.dryRun = dry
	}
}

// InstallDisableHooks disables hooks during installation.
func InstallDisableHooks(disable bool) InstallOption {
	return func(opts *options) {
		opts.disableHooks = disable
	}
}

// InstallDisableCRDHook disables CRD hook during installation.
func InstallDisableCRDHook(disable bool) InstallOption {
	return func(opts *options) {
		opts.disableCRDHook = disable
	}
}

// InstallReuseName will (if true) instruct Tiller to re-use an existing name.
func InstallReuseName(reuse bool) InstallOption {
	return func(opts *options) {
		opts.reuseName = reuse
	}
}

// InstallSubNotes will (if true) instruct Tiller to render SubChart Notes
func InstallSubNotes(enable bool) InstallOption {
	return func(opts *options) {
		opts.instReq.SubNotes = enable
	}
}

// UpgradeSubNotes will (if true) instruct Tiller to render SubChart Notes
func UpgradeSubNotes(enable bool) UpdateOption {
	return func(opts *options) {
		opts.updateReq.SubNotes = enable
	}
}

// RollbackDisableHooks will disable hooks for a rollback operation
func RollbackDisableHooks(disable bool) RollbackOption {
	return func(opts *options) {
		opts.disableHooks = disable
	}
}

// RollbackDryRun will (if true) execute a rollback as a dry run.
func RollbackDryRun(dry bool) RollbackOption {
	return func(opts *options) {
		opts.dryRun = dry
	}
}

// RollbackRecreate will (if true) recreate pods after rollback.
func RollbackRecreate(recreate bool) RollbackOption {
	return func(opts *options) {
		opts.recreate = recreate
	}
}

// RollbackForce will (if true) force resource update through delete/recreate if needed
func RollbackForce(force bool) RollbackOption {
	return func(opts *options) {
		opts.force = force
	}
}

// RollbackVersion sets the version of the release to deploy.
func RollbackVersion(ver int32) RollbackOption {
	return func(opts *options) {
		opts.rollbackReq.Version = ver
	}
}

// UpgradeDisableHooks will disable hooks for an upgrade operation.
func UpgradeDisableHooks(disable bool) UpdateOption {
	return func(opts *options) {
		opts.disableHooks = disable
	}
}

// UpgradeDryRun will (if true) execute an upgrade as a dry run.
func UpgradeDryRun(dry bool) UpdateOption {
	return func(opts *options) {
		opts.dryRun = dry
	}
}

// ResetValues will (if true) trigger resetting the values to their original state.
func ResetValues(reset bool) UpdateOption {
	return func(opts *options) {
		opts.resetValues = reset
	}
}

// ReuseValues will cause Tiller to reuse the values from the last release.
// This is ignored if ResetValues is true.
func ReuseValues(reuse bool) UpdateOption {
	return func(opts *options) {
		opts.reuseValues = reuse
	}
}

// UpgradeRecreate will (if true) recreate pods after upgrade.
func UpgradeRecreate(recreate bool) UpdateOption {
	return func(opts *options) {
		opts.recreate = recreate
	}
}

// UpgradeForce will (if true) force resource update through delete/recreate if needed
func UpgradeForce(force bool) UpdateOption {
	return func(opts *options) {
		opts.force = force
	}
}

// ContentOption allows setting optional attributes when
// performing a GetReleaseContent tiller rpc.
type ContentOption func(*options)

// ContentReleaseVersion will instruct Tiller to retrieve the content
// of a particular version of a release.
func ContentReleaseVersion(version int32) ContentOption {
	return func(opts *options) {
		opts.contentReq.Version = version
	}
}

// StatusOption allows setting optional attributes when
// performing a GetReleaseStatus tiller rpc.
type StatusOption func(*options)

// StatusReleaseVersion will instruct Tiller to retrieve the status
// of a particular version of a release.
func StatusReleaseVersion(version int32) StatusOption {
	return func(opts *options) {
		opts.statusReq.Version = version
	}
}

// DeleteOption allows setting optional attributes when
// performing a UninstallRelease tiller rpc.
type DeleteOption func(*options)

// VersionOption -- TODO
type VersionOption func(*options)

// UpdateOption allows specifying various settings
// configurable by the helm client user for overriding
// the defaults used when running the `helm upgrade` command.
type UpdateOption func(*options)

// RollbackOption allows specififying various settings configurable
// by the helm client user for overriding the defaults used when
// running the `helm rollback` command.
type RollbackOption func(*options)

// HistoryOption allows configuring optional request data for
// issuing a GetHistory rpc.
type HistoryOption func(*options)

// WithMaxHistory sets the max number of releases to return
// in a release history query.
func WithMaxHistory(max int32) HistoryOption {
	return func(opts *options) {
		opts.histReq.Max = max
	}
}

// NewContext creates a versioned context.
func NewContext() context.Context {
	md := metadata.Pairs("x-helm-api-client", version.GetVersion())
	return metadata.NewOutgoingContext(context.TODO(), md)
}

// ReleaseTestOption allows configuring optional request data for
// issuing a TestRelease rpc.
type ReleaseTestOption func(*options)
<|endoftext|>