<commit_before>package build

import (
	"fmt"
	"net/http"

	"github.com/gorilla/mux"
	"github.com/urfave/negroni"
	"github.com/velocity-ci/velocity/backend/api/auth"
	"github.com/velocity-ci/velocity/backend/api/middleware"
)

func (c Controller) addBuildRoutes(router *mux.Router) {
	// POST /v1/projects/{id}/commits/{commitHash}/tasks/{taskName}/builds
	router.Handle("/v1/projects/{projectID}/commits/{commitHash}/tasks/{taskName}/builds", negroni.New(
		auth.NewJWT(c.render),
		negroni.Wrap(http.HandlerFunc(c.postProjectCommitTaskBuildsHandler)),
	)).Methods("POST")

	// POST /v1/tasks/{taskUUID}/builds
	router.Handle("/v1/tasks/{taskUUID}/builds", negroni.New(
		auth.NewJWT(c.render),
		negroni.Wrap(http.HandlerFunc(c.postTaskByUUIDBuildsHandler)),
	)).Methods("POST")

	// GET /v1/projects/{projectID}/builds
	router.Handle("/v1/projects/{projectID}/builds", negroni.New(
		auth.NewJWT(c.render),
		negroni.Wrap(http.HandlerFunc(c.getProjectBuildsHandler)),
	)).Methods("GET")

	// GET /v1/projects/{projectID}/commits/{commitHash}/builds
	router.Handle("/v1/projects/{projectID}/commits/{commitHash}/builds", negroni.New(
		auth.NewJWT(c.render),
		negroni.Wrap(http.HandlerFunc(c.getProjectCommitBuildsHandler)),
	)).Methods("GET")

	// GET /v1/projects/{projectID}/commits/{commitHash}/tasks/{taskName}/builds
	router.Handle("/v1/projects/{projectID}/commits/{commitHash}/tasks/{taskName}/builds", negroni.New(
		auth.NewJWT(c.render),
		negroni.Wrap(http.HandlerFunc(c.getProjectCommitTaskBuildsHandler)),
	)).Methods("GET")

	// GET /v1/commits/{commitUUID}/builds
	router.Handle("/v1/commits/{commitUUID}/builds", negroni.New(
		auth.NewJWT(c.render),
		negroni.Wrap(http.HandlerFunc(c.getCommitByUUIDBuildsHandler)),
	)).Methods("GET")

	// GET /v1/tasks/{taskUUID}/builds
	router.Handle("/v1/tasks/{taskUUID}/builds", negroni.New(
		auth.NewJWT(c.render),
		negroni.Wrap(http.HandlerFunc(c.getTaskByUUIDBuildsHandler)),
	)).Methods("GET")

	// GET /v1/builds/{buildUUID}
	router.Handle("/v1/builds/{buildUUID}", negroni.New(
		auth.NewJWT(c.render),
		negroni.Wrap(http.HandlerFunc(c.getBuildByUUIDHandler)),
	)).Methods("GET")
}

func (c Controller) postProjectCommitTaskBuildsHandler(w http.ResponseWriter, r *http.Request) {
	reqVars := mux.Vars(r)
	reqProjectID := reqVars["projectID"]
	reqCommitHash := reqVars["commitHash"]
	reqTaskName := reqVars["taskName"]

	p, err := c.projectManager.GetByID(reqProjectID)
	if handleResourceError(c.render, w, err, fmt.Sprintf("could not find project %s", reqProjectID)) {
		return
	}

	cm, err := c.commitManager.GetCommitByProjectIDAndCommitHash(p.ID, reqCommitHash)
	if handleResourceError(c.render, w, err, fmt.Sprintf("could not find commit %s", reqCommitHash)) {
		return
	}

	task, err := c.taskManager.GetByCommitIDAndTaskName(cm.ID, reqTaskName)
	if handleResourceError(c.render, w, err, fmt.Sprintf("could not find task %s", reqTaskName)) {
		return
	}

	build, err := c.resolver.BuildFromRequest(r.Body, task)
	if err != nil {
		middleware.HandleRequestError(err, w, c.render)
		return
	}

	c.manager.CreateBuild(build)

	c.render.JSON(w, http.StatusCreated, NewResponseBuild(build))

	// queuedBuild := NewQueuedBuild(build, project.ID, commit.Hash)
	// c.manager.QueueBuild(queuedBuild)
}

func (c Controller) postTaskByUUIDBuildsHandler(w http.ResponseWriter, r *http.Request) {
	reqVars := mux.Vars(r)
	reqTaskUUID := reqVars["taskUUID"]

	task, err := c.taskManager.GetByTaskID(reqTaskUUID)
	if handleResourceError(c.render, w, err, fmt.Sprintf("could not find task %s", reqTaskUUID)) {
		return
	}

	build, err := c.resolver.BuildFromRequest(r.Body, task)
	if err != nil {
		middleware.HandleRequestError(err, w, c.render)
		return
	}

	c.manager.CreateBuild(build)

	c.render.JSON(w, http.StatusCreated, NewResponseBuild(build))
}

func (c Controller) getProjectBuildsHandler(w http.ResponseWriter, r *http.Request) {
	reqVars := mux.Vars(r)
	reqProjectID := reqVars["projectID"]

	p, err := c.projectManager.GetByID(reqProjectID)
	if handleResourceError(c.render, w, err, fmt.Sprintf("could not find project %s", reqProjectID)) {
		return
	}

	opts := BuildQueryOptsFromRequest(r)

	builds, count := c.manager.GetBuildsByProjectID(p.ID, opts)

	respBuilds := []ResponseBuild{}
	for _, b := range builds {
		respBuilds = append(respBuilds, NewResponseBuild(b))
	}

	c.render.JSON(w, http.StatusOK, BuildManyResponse{
		Total:  count,
		Result: respBuilds,
	})
}

func (c Controller) getProjectCommitBuildsHandler(w http.ResponseWriter, r *http.Request) {
	reqVars := mux.Vars(r)
	reqProjectID := reqVars["projectID"]
	reqCommitHash := reqVars["commitHash"]

	p, err := c.projectManager.GetByID(reqProjectID)
	if handleResourceError(c.render, w, err, fmt.Sprintf("could not find project %s", reqProjectID)) {
		return
	}

	cm, err := c.commitManager.GetCommitByProjectIDAndCommitHash(p.ID, reqCommitHash)
	if handleResourceError(c.render, w, err, fmt.Sprintf("could not find commit %s", reqCommitHash)) {
		return
	}

	opts := BuildQueryOptsFromRequest(r)
	builds, count := c.manager.GetBuildsByCommitID(cm.ID, opts)

	respBuilds := []ResponseBuild{}
	for _, b := range builds {
		respBuilds = append(respBuilds, NewResponseBuild(b))
	}
	c.render.JSON(w, http.StatusOK, BuildManyResponse{
		Total:  count,
		Result: respBuilds,
	})
}

func (c Controller) getProjectCommitTaskBuildsHandler(w http.ResponseWriter, r *http.Request) {
	reqVars := mux.Vars(r)
	reqProjectID := reqVars["projectID"]
	reqCommitHash := reqVars["commitHash"]
	reqTaskName := reqVars["taskName"]

	p, err := c.projectManager.GetByID(reqProjectID)
	if handleResourceError(c.render, w, err, fmt.Sprintf("could not find project %s", reqProjectID)) {
		return
	}

	cm, err := c.commitManager.GetCommitByProjectIDAndCommitHash(p.ID, reqCommitHash)
	if handleResourceError(c.render, w, err, fmt.Sprintf("could not find commit %s", reqCommitHash)) {
		return
	}

	task, err := c.taskManager.GetByCommitIDAndTaskName(cm.ID, reqTaskName)
	if handleResourceError(c.render, w, err, fmt.Sprintf("could not find task %s", reqTaskName)) {
		return
	}

	opts := BuildQueryOptsFromRequest(r)

	builds, count := c.manager.GetBuildsByTaskID(task.ID, opts)

	respBuilds := []ResponseBuild{}
	for _, b := range builds {
		respBuilds = append(respBuilds, NewResponseBuild(b))
	}

	c.render.JSON(w, http.StatusOK, BuildManyResponse{
		Total:  count,
		Result: respBuilds,
	})
}

func (c Controller) getCommitByUUIDBuildsHandler(w http.ResponseWriter, r *http.Request) {
	reqVars := mux.Vars(r)
	reqCommitUUID := reqVars["commitUUID"]

	cm, err := c.commitManager.GetCommitByCommitID(reqCommitUUID)
	if handleResourceError(c.render, w, err, fmt.Sprintf("could not find commit %s", reqCommitUUID)) {
		return
	}

	opts := BuildQueryOptsFromRequest(r)

	builds, count := c.manager.GetBuildsByCommitID(cm.ID, opts)

	respBuilds := []ResponseBuild{}
	for _, b := range builds {
		respBuilds = append(respBuilds, NewResponseBuild(b))
	}

	c.render.JSON(w, http.StatusOK, BuildManyResponse{
		Total:  count,
		Result: respBuilds,
	})
}

func (c Controller) getTaskByUUIDBuildsHandler(w http.ResponseWriter, r *http.Request) {
	reqVars := mux.Vars(r)
	reqTaskUUID := reqVars["taskUUID"]

	task, err := c.taskManager.GetByTaskID(reqTaskUUID)
	if handleResourceError(c.render, w, err, fmt.Sprintf("could not find task %s", reqTaskUUID)) {
		return
	}

	opts := BuildQueryOptsFromRequest(r)

	builds, count := c.manager.GetBuildsByTaskID(task.ID, opts)

	respBuilds := []ResponseBuild{}
	for _, b := range builds {
		respBuilds = append(respBuilds, NewResponseBuild(b))
	}

	c.render.JSON(w, http.StatusOK, BuildManyResponse{
		Total:  count,
		Result: respBuilds,
	})
}

func (c Controller) getBuildByUUIDHandler(w http.ResponseWriter, r *http.Request) {
	reqVars := mux.Vars(r)
	reqBuildUUID := reqVars["buildUUID"]

	build, err := c.manager.GetBuildByBuildID(reqBuildUUID)
	if handleResourceError(c.render, w, err, fmt.Sprintf("could not find build %s", reqBuildUUID)) {
		return
	}

	c.render.JSON(w, http.StatusOK, NewResponseBuild(build))
}
<commit_msg>[backend] Should be returning steps from POST build<commit_after>package build

import (
	"fmt"
	"net/http"

	"github.com/gorilla/mux"
	"github.com/urfave/negroni"
	"github.com/velocity-ci/velocity/backend/api/auth"
	"github.com/velocity-ci/velocity/backend/api/middleware"
)

func (c Controller) addBuildRoutes(router *mux.Router) {
	// POST /v1/projects/{id}/commits/{commitHash}/tasks/{taskName}/builds
	router.Handle("/v1/projects/{projectID}/commits/{commitHash}/tasks/{taskName}/builds", negroni.New(
		auth.NewJWT(c.render),
		negroni.Wrap(http.HandlerFunc(c.postProjectCommitTaskBuildsHandler)),
	)).Methods("POST")

	// POST /v1/tasks/{taskUUID}/builds
	router.Handle("/v1/tasks/{taskUUID}/builds", negroni.New(
		auth.NewJWT(c.render),
		negroni.Wrap(http.HandlerFunc(c.postTaskByUUIDBuildsHandler)),
	)).Methods("POST")

	// GET /v1/projects/{projectID}/builds
	router.Handle("/v1/projects/{projectID}/builds", negroni.New(
		auth.NewJWT(c.render),
		negroni.Wrap(http.HandlerFunc(c.getProjectBuildsHandler)),
	)).Methods("GET")

	// GET /v1/projects/{projectID}/commits/{commitHash}/builds
	router.Handle("/v1/projects/{projectID}/commits/{commitHash}/builds", negroni.New(
		auth.NewJWT(c.render),
		negroni.Wrap(http.HandlerFunc(c.getProjectCommitBuildsHandler)),
	)).Methods("GET")

	// GET /v1/projects/{projectID}/commits/{commitHash}/tasks/{taskName}/builds
	router.Handle("/v1/projects/{projectID}/commits/{commitHash}/tasks/{taskName}/builds", negroni.New(
		auth.NewJWT(c.render),
		negroni.Wrap(http.HandlerFunc(c.getProjectCommitTaskBuildsHandler)),
	)).Methods("GET")

	// GET /v1/commits/{commitUUID}/builds
	router.Handle("/v1/commits/{commitUUID}/builds", negroni.New(
		auth.NewJWT(c.render),
		negroni.Wrap(http.HandlerFunc(c.getCommitByUUIDBuildsHandler)),
	)).Methods("GET")

	// GET /v1/tasks/{taskUUID}/builds
	router.Handle("/v1/tasks/{taskUUID}/builds", negroni.New(
		auth.NewJWT(c.render),
		negroni.Wrap(http.HandlerFunc(c.getTaskByUUIDBuildsHandler)),
	)).Methods("GET")

	// GET /v1/builds/{buildUUID}
	router.Handle("/v1/builds/{buildUUID}", negroni.New(
		auth.NewJWT(c.render),
		negroni.Wrap(http.HandlerFunc(c.getBuildByUUIDHandler)),
	)).Methods("GET")
}

func (c Controller) postProjectCommitTaskBuildsHandler(w http.ResponseWriter, r *http.Request) {
	reqVars := mux.Vars(r)
	reqProjectID := reqVars["projectID"]
	reqCommitHash := reqVars["commitHash"]
	reqTaskName := reqVars["taskName"]

	p, err := c.projectManager.GetByID(reqProjectID)
	if handleResourceError(c.render, w, err, fmt.Sprintf("could not find project %s", reqProjectID)) {
		return
	}

	cm, err := c.commitManager.GetCommitByProjectIDAndCommitHash(p.ID, reqCommitHash)
	if handleResourceError(c.render, w, err, fmt.Sprintf("could not find commit %s", reqCommitHash)) {
		return
	}

	task, err := c.taskManager.GetByCommitIDAndTaskName(cm.ID, reqTaskName)
	if handleResourceError(c.render, w, err, fmt.Sprintf("could not find task %s", reqTaskName)) {
		return
	}

	build, err := c.resolver.BuildFromRequest(r.Body, task)
	if err != nil {
		middleware.HandleRequestError(err, w, c.render)
		return
	}

	build = c.manager.CreateBuild(build)

	c.render.JSON(w, http.StatusCreated, NewResponseBuild(build))

	// queuedBuild := NewQueuedBuild(build, project.ID, commit.Hash)
	// c.manager.QueueBuild(queuedBuild)
}

func (c Controller) postTaskByUUIDBuildsHandler(w http.ResponseWriter, r *http.Request) {
	reqVars := mux.Vars(r)
	reqTaskUUID := reqVars["taskUUID"]

	task, err := c.taskManager.GetByTaskID(reqTaskUUID)
	if handleResourceError(c.render, w, err, fmt.Sprintf("could not find task %s", reqTaskUUID)) {
		return
	}

	build, err := c.resolver.BuildFromRequest(r.Body, task)
	if err != nil {
		middleware.HandleRequestError(err, w, c.render)
		return
	}

	c.manager.CreateBuild(build)

	c.render.JSON(w, http.StatusCreated, NewResponseBuild(build))
}

func (c Controller) getProjectBuildsHandler(w http.ResponseWriter, r *http.Request) {
	reqVars := mux.Vars(r)
	reqProjectID := reqVars["projectID"]

	p, err := c.projectManager.GetByID(reqProjectID)
	if handleResourceError(c.render, w, err, fmt.Sprintf("could not find project %s", reqProjectID)) {
		return
	}

	opts := BuildQueryOptsFromRequest(r)

	builds, count := c.manager.GetBuildsByProjectID(p.ID, opts)

	respBuilds := []ResponseBuild{}
	for _, b := range builds {
		respBuilds = append(respBuilds, NewResponseBuild(b))
	}

	c.render.JSON(w, http.StatusOK, BuildManyResponse{
		Total:  count,
		Result: respBuilds,
	})
}

func (c Controller) getProjectCommitBuildsHandler(w http.ResponseWriter, r *http.Request) {
	reqVars := mux.Vars(r)
	reqProjectID := reqVars["projectID"]
	reqCommitHash := reqVars["commitHash"]

	p, err := c.projectManager.GetByID(reqProjectID)
	if handleResourceError(c.render, w, err, fmt.Sprintf("could not find project %s", reqProjectID)) {
		return
	}

	cm, err := c.commitManager.GetCommitByProjectIDAndCommitHash(p.ID, reqCommitHash)
	if handleResourceError(c.render, w, err, fmt.Sprintf("could not find commit %s", reqCommitHash)) {
		return
	}

	opts := BuildQueryOptsFromRequest(r)
	builds, count := c.manager.GetBuildsByCommitID(cm.ID, opts)

	respBuilds := []ResponseBuild{}
	for _, b := range builds {
		respBuilds = append(respBuilds, NewResponseBuild(b))
	}
	c.render.JSON(w, http.StatusOK, BuildManyResponse{
		Total:  count,
		Result: respBuilds,
	})
}

func (c Controller) getProjectCommitTaskBuildsHandler(w http.ResponseWriter, r *http.Request) {
	reqVars := mux.Vars(r)
	reqProjectID := reqVars["projectID"]
	reqCommitHash := reqVars["commitHash"]
	reqTaskName := reqVars["taskName"]

	p, err := c.projectManager.GetByID(reqProjectID)
	if handleResourceError(c.render, w, err, fmt.Sprintf("could not find project %s", reqProjectID)) {
		return
	}

	cm, err := c.commitManager.GetCommitByProjectIDAndCommitHash(p.ID, reqCommitHash)
	if handleResourceError(c.render, w, err, fmt.Sprintf("could not find commit %s", reqCommitHash)) {
		return
	}

	task, err := c.taskManager.GetByCommitIDAndTaskName(cm.ID, reqTaskName)
	if handleResourceError(c.render, w, err, fmt.Sprintf("could not find task %s", reqTaskName)) {
		return
	}

	opts := BuildQueryOptsFromRequest(r)

	builds, count := c.manager.GetBuildsByTaskID(task.ID, opts)

	respBuilds := []ResponseBuild{}
	for _, b := range builds {
		respBuilds = append(respBuilds, NewResponseBuild(b))
	}

	c.render.JSON(w, http.StatusOK, BuildManyResponse{
		Total:  count,
		Result: respBuilds,
	})
}

func (c Controller) getCommitByUUIDBuildsHandler(w http.ResponseWriter, r *http.Request) {
	reqVars := mux.Vars(r)
	reqCommitUUID := reqVars["commitUUID"]

	cm, err := c.commitManager.GetCommitByCommitID(reqCommitUUID)
	if handleResourceError(c.render, w, err, fmt.Sprintf("could not find commit %s", reqCommitUUID)) {
		return
	}

	opts := BuildQueryOptsFromRequest(r)

	builds, count := c.manager.GetBuildsByCommitID(cm.ID, opts)

	respBuilds := []ResponseBuild{}
	for _, b := range builds {
		respBuilds = append(respBuilds, NewResponseBuild(b))
	}

	c.render.JSON(w, http.StatusOK, BuildManyResponse{
		Total:  count,
		Result: respBuilds,
	})
}

func (c Controller) getTaskByUUIDBuildsHandler(w http.ResponseWriter, r *http.Request) {
	reqVars := mux.Vars(r)
	reqTaskUUID := reqVars["taskUUID"]

	task, err := c.taskManager.GetByTaskID(reqTaskUUID)
	if handleResourceError(c.render, w, err, fmt.Sprintf("could not find task %s", reqTaskUUID)) {
		return
	}

	opts := BuildQueryOptsFromRequest(r)

	builds, count := c.manager.GetBuildsByTaskID(task.ID, opts)

	respBuilds := []ResponseBuild{}
	for _, b := range builds {
		respBuilds = append(respBuilds, NewResponseBuild(b))
	}

	c.render.JSON(w, http.StatusOK, BuildManyResponse{
		Total:  count,
		Result: respBuilds,
	})
}

func (c Controller) getBuildByUUIDHandler(w http.ResponseWriter, r *http.Request) {
	reqVars := mux.Vars(r)
	reqBuildUUID := reqVars["buildUUID"]

	build, err := c.manager.GetBuildByBuildID(reqBuildUUID)
	if handleResourceError(c.render, w, err, fmt.Sprintf("could not find build %s", reqBuildUUID)) {
		return
	}

	c.render.JSON(w, http.StatusOK, NewResponseBuild(build))
}
<|endoftext|>


<commit_before>/*

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 controllers

import (
	"context"
	"fmt"
	"time"

	"github.com/aws/aws-sdk-go/aws"
	"github.com/pkg/errors"
	corev1 "k8s.io/api/core/v1"
	"k8s.io/client-go/tools/record"
	"k8s.io/client-go/util/workqueue"
	"sigs.k8s.io/aws-load-balancer-controller/controllers/elbv2/eventhandlers"
	"sigs.k8s.io/aws-load-balancer-controller/pkg/config"
	"sigs.k8s.io/aws-load-balancer-controller/pkg/k8s"
	"sigs.k8s.io/aws-load-balancer-controller/pkg/runtime"
	"sigs.k8s.io/aws-load-balancer-controller/pkg/targetgroupbinding"
	"sigs.k8s.io/controller-runtime/pkg/controller"
	"sigs.k8s.io/controller-runtime/pkg/source"

	"github.com/go-logr/logr"
	ctrl "sigs.k8s.io/controller-runtime"
	"sigs.k8s.io/controller-runtime/pkg/client"

	elbv2api "sigs.k8s.io/aws-load-balancer-controller/apis/elbv2/v1beta1"
)

const (
	targetGroupBindingFinalizer = "elbv2.k8s.aws/resources"
	controllerName              = "targetGroupBinding"
)

// NewTargetGroupBindingReconciler constructs new targetGroupBindingReconciler
func NewTargetGroupBindingReconciler(k8sClient client.Client, eventRecorder record.EventRecorder, finalizerManager k8s.FinalizerManager,
	tgbResourceManager targetgroupbinding.ResourceManager, config config.ControllerConfig,
	logger logr.Logger) *targetGroupBindingReconciler {

	return &targetGroupBindingReconciler{
		k8sClient:          k8sClient,
		eventRecorder:      eventRecorder,
		finalizerManager:   finalizerManager,
		tgbResourceManager: tgbResourceManager,
		logger:             logger,

		maxConcurrentReconciles:    config.TargetGroupBindingMaxConcurrentReconciles,
		maxExponentialBackoffDelay: config.TargetGroupBindingMaxExponentialBackoffDelay,
	}
}

// targetGroupBindingReconciler reconciles a TargetGroupBinding object
type targetGroupBindingReconciler struct {
	k8sClient          client.Client
	eventRecorder      record.EventRecorder
	finalizerManager   k8s.FinalizerManager
	tgbResourceManager targetgroupbinding.ResourceManager
	logger             logr.Logger

	maxConcurrentReconciles    int
	maxExponentialBackoffDelay time.Duration
}

// +kubebuilder:rbac:groups=elbv2.k8s.aws,resources=targetgroupbindings,verbs=get;list;watch;update;patch;create;delete
// +kubebuilder:rbac:groups=elbv2.k8s.aws,resources=targetgroupbindings/status,verbs=update;patch
// +kubebuilder:rbac:groups="",resources=pods,verbs=get;list;watch
// +kubebuilder:rbac:groups="",resources=pods/status,verbs=update;patch
// +kubebuilder:rbac:groups="",resources=nodes,verbs=get;list;watch
// +kubebuilder:rbac:groups="",resources=endpoints,verbs=get;list;watch
// +kubebuilder:rbac:groups="",resources=secrets,verbs=get;list;watch
// +kubebuilder:rbac:groups="",resources=namespaces,verbs=get;list;watch
// +kubebuilder:rbac:groups="",resources=events,verbs=create;patch

func (r *targetGroupBindingReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
	return runtime.HandleReconcileError(r.reconcile(req), r.logger)
}

func (r *targetGroupBindingReconciler) reconcile(req ctrl.Request) error {
	ctx := context.Background()
	tgb := &elbv2api.TargetGroupBinding{}
	if err := r.k8sClient.Get(ctx, req.NamespacedName, tgb); err != nil {
		return client.IgnoreNotFound(err)
	}

	if !tgb.DeletionTimestamp.IsZero() {
		return r.cleanupTargetGroupBinding(ctx, tgb)
	}
	return r.reconcileTargetGroupBinding(ctx, tgb)
}

func (r *targetGroupBindingReconciler) reconcileTargetGroupBinding(ctx context.Context, tgb *elbv2api.TargetGroupBinding) error {
	if err := r.finalizerManager.AddFinalizers(ctx, tgb, targetGroupBindingFinalizer); err != nil {
		r.eventRecorder.Event(tgb, corev1.EventTypeWarning, k8s.TargetGroupBindingEventReasonFailedAddFinalizer, fmt.Sprintf("Failed add finalizer due to %v", err))
		return err
	}
	if err := r.tgbResourceManager.Reconcile(ctx, tgb); err != nil {
		return err
	}
	if err := r.updateTargetGroupBindingStatus(ctx, tgb); err != nil {
		r.eventRecorder.Event(tgb, corev1.EventTypeWarning, k8s.TargetGroupBindingEventReasonFailedUpdateStatus, fmt.Sprintf("Failed update status due to %v", err))
		return err
	}

	r.eventRecorder.Event(tgb, corev1.EventTypeNormal, k8s.TargetGroupBindingEventReasonSuccessfullyReconciled, "Successfully reconciled")
	return nil
}

func (r *targetGroupBindingReconciler) cleanupTargetGroupBinding(ctx context.Context, tgb *elbv2api.TargetGroupBinding) error {
	if k8s.HasFinalizer(tgb, targetGroupBindingFinalizer) {
		if err := r.tgbResourceManager.Cleanup(ctx, tgb); err != nil {
			r.eventRecorder.Event(tgb, corev1.EventTypeWarning, k8s.TargetGroupBindingEventReasonFailedCleanup, fmt.Sprintf("Failed cleanup due to %v", err))
			return err
		}
		if err := r.finalizerManager.RemoveFinalizers(ctx, tgb, targetGroupBindingFinalizer); err != nil {
			r.eventRecorder.Event(tgb, corev1.EventTypeWarning, k8s.TargetGroupBindingEventReasonFailedRemoveFinalizer, fmt.Sprintf("Failed remove finalizer due to %v", err))
			return err
		}
	}
	return nil
}

func (r *targetGroupBindingReconciler) updateTargetGroupBindingStatus(ctx context.Context, tgb *elbv2api.TargetGroupBinding) error {
	if aws.Int64Value(tgb.Status.ObservedGeneration) == tgb.Generation {
		return nil
	}
	tgbOld := tgb.DeepCopy()
	tgb.Status.ObservedGeneration = aws.Int64(tgb.Generation)
	if err := r.k8sClient.Status().Patch(ctx, tgb, client.MergeFrom(tgbOld)); err != nil {
		return errors.Wrapf(err, "failed to update targetGroupBinding status: %v", k8s.NamespacedName(tgb))
	}
	return nil
}

func (r *targetGroupBindingReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager) error {
	if err := r.setupIndexes(ctx, mgr.GetFieldIndexer()); err != nil {
		return err
	}

	svcEventHandler := eventhandlers.NewEnqueueRequestsForServiceEvent(r.k8sClient,
		r.logger.WithName("eventHandlers").WithName("service"))
	epsEventsHandler := eventhandlers.NewEnqueueRequestsForEndpointsEvent(r.k8sClient,
		r.logger.WithName("eventHandlers").WithName("endpoints"))
	nodeEventsHandler := eventhandlers.NewEnqueueRequestsForNodeEvent(r.k8sClient,
		r.logger.WithName("eventHandlers").WithName("node"))
	return ctrl.NewControllerManagedBy(mgr).
		For(&elbv2api.TargetGroupBinding{}).
		Named(controllerName).
		Watches(&source.Kind{Type: &corev1.Service{}}, svcEventHandler).
		Watches(&source.Kind{Type: &corev1.Endpoints{}}, epsEventsHandler).
		Watches(&source.Kind{Type: &corev1.Node{}}, nodeEventsHandler).
		WithOptions(controller.Options{
			MaxConcurrentReconciles: r.maxConcurrentReconciles,
			RateLimiter:             workqueue.NewItemExponentialFailureRateLimiter(time.Millisecond, r.maxExponentialBackoffDelay)}).
		Complete(r)
}

func (r *targetGroupBindingReconciler) setupIndexes(ctx context.Context, fieldIndexer client.FieldIndexer) error {
	if err := fieldIndexer.IndexField(ctx, &elbv2api.TargetGroupBinding{},
		targetgroupbinding.IndexKeyServiceRefName, targetgroupbinding.IndexFuncServiceRefName); err != nil {
		return err
	}
	return nil
}
<commit_msg>use default controller limiter<commit_after>/*

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 controllers

import (
	"context"
	"fmt"
	"time"

	"github.com/aws/aws-sdk-go/aws"
	"github.com/pkg/errors"
	corev1 "k8s.io/api/core/v1"
	"k8s.io/client-go/tools/record"
	"k8s.io/client-go/util/workqueue"
	"sigs.k8s.io/aws-load-balancer-controller/controllers/elbv2/eventhandlers"
	"sigs.k8s.io/aws-load-balancer-controller/pkg/config"
	"sigs.k8s.io/aws-load-balancer-controller/pkg/k8s"
	"sigs.k8s.io/aws-load-balancer-controller/pkg/runtime"
	"sigs.k8s.io/aws-load-balancer-controller/pkg/targetgroupbinding"
	"sigs.k8s.io/controller-runtime/pkg/controller"
	"sigs.k8s.io/controller-runtime/pkg/source"

	"github.com/go-logr/logr"
	ctrl "sigs.k8s.io/controller-runtime"
	"sigs.k8s.io/controller-runtime/pkg/client"

	elbv2api "sigs.k8s.io/aws-load-balancer-controller/apis/elbv2/v1beta1"
)

const (
	targetGroupBindingFinalizer = "elbv2.k8s.aws/resources"
	controllerName              = "targetGroupBinding"
)

// NewTargetGroupBindingReconciler constructs new targetGroupBindingReconciler
func NewTargetGroupBindingReconciler(k8sClient client.Client, eventRecorder record.EventRecorder, finalizerManager k8s.FinalizerManager,
	tgbResourceManager targetgroupbinding.ResourceManager, config config.ControllerConfig,
	logger logr.Logger) *targetGroupBindingReconciler {

	return &targetGroupBindingReconciler{
		k8sClient:          k8sClient,
		eventRecorder:      eventRecorder,
		finalizerManager:   finalizerManager,
		tgbResourceManager: tgbResourceManager,
		logger:             logger,

		maxConcurrentReconciles:    config.TargetGroupBindingMaxConcurrentReconciles,
		maxExponentialBackoffDelay: config.TargetGroupBindingMaxExponentialBackoffDelay,
	}
}

// targetGroupBindingReconciler reconciles a TargetGroupBinding object
type targetGroupBindingReconciler struct {
	k8sClient          client.Client
	eventRecorder      record.EventRecorder
	finalizerManager   k8s.FinalizerManager
	tgbResourceManager targetgroupbinding.ResourceManager
	logger             logr.Logger

	maxConcurrentReconciles    int
	maxExponentialBackoffDelay time.Duration
}

// +kubebuilder:rbac:groups=elbv2.k8s.aws,resources=targetgroupbindings,verbs=get;list;watch;update;patch;create;delete
// +kubebuilder:rbac:groups=elbv2.k8s.aws,resources=targetgroupbindings/status,verbs=update;patch
// +kubebuilder:rbac:groups="",resources=pods,verbs=get;list;watch
// +kubebuilder:rbac:groups="",resources=pods/status,verbs=update;patch
// +kubebuilder:rbac:groups="",resources=nodes,verbs=get;list;watch
// +kubebuilder:rbac:groups="",resources=endpoints,verbs=get;list;watch
// +kubebuilder:rbac:groups="",resources=secrets,verbs=get;list;watch
// +kubebuilder:rbac:groups="",resources=namespaces,verbs=get;list;watch
// +kubebuilder:rbac:groups="",resources=events,verbs=create;patch

func (r *targetGroupBindingReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
	return runtime.HandleReconcileError(r.reconcile(req), r.logger)
}

func (r *targetGroupBindingReconciler) reconcile(req ctrl.Request) error {
	ctx := context.Background()
	tgb := &elbv2api.TargetGroupBinding{}
	if err := r.k8sClient.Get(ctx, req.NamespacedName, tgb); err != nil {
		return client.IgnoreNotFound(err)
	}

	if !tgb.DeletionTimestamp.IsZero() {
		return r.cleanupTargetGroupBinding(ctx, tgb)
	}
	return r.reconcileTargetGroupBinding(ctx, tgb)
}

func (r *targetGroupBindingReconciler) reconcileTargetGroupBinding(ctx context.Context, tgb *elbv2api.TargetGroupBinding) error {
	if err := r.finalizerManager.AddFinalizers(ctx, tgb, targetGroupBindingFinalizer); err != nil {
		r.eventRecorder.Event(tgb, corev1.EventTypeWarning, k8s.TargetGroupBindingEventReasonFailedAddFinalizer, fmt.Sprintf("Failed add finalizer due to %v", err))
		return err
	}
	if err := r.tgbResourceManager.Reconcile(ctx, tgb); err != nil {
		return err
	}
	if err := r.updateTargetGroupBindingStatus(ctx, tgb); err != nil {
		r.eventRecorder.Event(tgb, corev1.EventTypeWarning, k8s.TargetGroupBindingEventReasonFailedUpdateStatus, fmt.Sprintf("Failed update status due to %v", err))
		return err
	}

	r.eventRecorder.Event(tgb, corev1.EventTypeNormal, k8s.TargetGroupBindingEventReasonSuccessfullyReconciled, "Successfully reconciled")
	return nil
}

func (r *targetGroupBindingReconciler) cleanupTargetGroupBinding(ctx context.Context, tgb *elbv2api.TargetGroupBinding) error {
	if k8s.HasFinalizer(tgb, targetGroupBindingFinalizer) {
		if err := r.tgbResourceManager.Cleanup(ctx, tgb); err != nil {
			r.eventRecorder.Event(tgb, corev1.EventTypeWarning, k8s.TargetGroupBindingEventReasonFailedCleanup, fmt.Sprintf("Failed cleanup due to %v", err))
			return err
		}
		if err := r.finalizerManager.RemoveFinalizers(ctx, tgb, targetGroupBindingFinalizer); err != nil {
			r.eventRecorder.Event(tgb, corev1.EventTypeWarning, k8s.TargetGroupBindingEventReasonFailedRemoveFinalizer, fmt.Sprintf("Failed remove finalizer due to %v", err))
			return err
		}
	}
	return nil
}

func (r *targetGroupBindingReconciler) updateTargetGroupBindingStatus(ctx context.Context, tgb *elbv2api.TargetGroupBinding) error {
	if aws.Int64Value(tgb.Status.ObservedGeneration) == tgb.Generation {
		return nil
	}
	tgbOld := tgb.DeepCopy()
	tgb.Status.ObservedGeneration = aws.Int64(tgb.Generation)
	if err := r.k8sClient.Status().Patch(ctx, tgb, client.MergeFrom(tgbOld)); err != nil {
		return errors.Wrapf(err, "failed to update targetGroupBinding status: %v", k8s.NamespacedName(tgb))
	}
	return nil
}

func (r *targetGroupBindingReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager) error {
	if err := r.setupIndexes(ctx, mgr.GetFieldIndexer()); err != nil {
		return err
	}

	svcEventHandler := eventhandlers.NewEnqueueRequestsForServiceEvent(r.k8sClient,
		r.logger.WithName("eventHandlers").WithName("service"))
	epsEventsHandler := eventhandlers.NewEnqueueRequestsForEndpointsEvent(r.k8sClient,
		r.logger.WithName("eventHandlers").WithName("endpoints"))
	nodeEventsHandler := eventhandlers.NewEnqueueRequestsForNodeEvent(r.k8sClient,
		r.logger.WithName("eventHandlers").WithName("node"))
	return ctrl.NewControllerManagedBy(mgr).
		For(&elbv2api.TargetGroupBinding{}).
		Named(controllerName).
		Watches(&source.Kind{Type: &corev1.Service{}}, svcEventHandler).
		Watches(&source.Kind{Type: &corev1.Endpoints{}}, epsEventsHandler).
		Watches(&source.Kind{Type: &corev1.Node{}}, nodeEventsHandler).
		WithOptions(controller.Options{
			MaxConcurrentReconciles: r.maxConcurrentReconciles,
			RateLimiter:             workqueue.NewItemExponentialFailureRateLimiter(5*time.Millisecond, r.maxExponentialBackoffDelay)}).
		Complete(r)
}

func (r *targetGroupBindingReconciler) setupIndexes(ctx context.Context, fieldIndexer client.FieldIndexer) error {
	if err := fieldIndexer.IndexField(ctx, &elbv2api.TargetGroupBinding{},
		targetgroupbinding.IndexKeyServiceRefName, targetgroupbinding.IndexFuncServiceRefName); err != nil {
		return err
	}
	return nil
}
<|endoftext|>


<commit_before>package libgodelbrot

type RedscalePalette CachePalette

func NewRedscalePalette(iterateLimit uint8) RedscalePalette {
    return NewCachePalette(iterateLimit, &redscaleCacher)
}

// Cache redscale colour values
func redscaleCacher(limit uint8, index uint8) color.NRGBA {
    return  color.NRGBA{
        R: limit - index,
        G: 0,
        B: 0,
        A: 255,
    }
}<commit_msg>Move up scale increments of intensity max / limit<commit_after>package libgodelbrot

type RedscalePalette CachePalette

func NewRedscalePalette(iterateLimit uint8) RedscalePalette {
    return NewCachePalette(iterateLimit, &redscaleCacher)
}

// Cache redscale colour values
func redscaleCacher(limit uint8, index uint8) color.NRGBA {
    return  color.NRGBA{
        R: index * (255 / limit),
        G: 0,
        B: 0,
        A: 255,
    }
}<|endoftext|>


<commit_before>package main

import (
	"io/ioutil"
	"log"
	"os"
	"os/exec"
	"path/filepath"
)

// InstallInfo is
type InstallInfo struct {
	url  string
	name string
}

// Install runs by gobou install hoge/foo
func (ii *InstallInfo) Install(pluginDir, cacheDir, pluginConfigDir string) {
	gurl, err := parseInstallURL(ii.url)
	srcDir := filepath.Join(cacheDir, "src")
	src := getSrcPath(ii.name, srcDir)
	dist := getDistPath(ii.name, pluginDir)
	if err != nil {
		return
	}
	err = clone(gurl, src)
	if err != nil {
		log.Fatalln(err)
		return
	}
	err = build(src, dist)
	if err != nil {
		log.Fatalln(err)
		return
	}

	err = initConfigFile(filepath.Join(pluginConfigDir, ii.name))
	if err != nil {
		log.Fatalln(err)
		return
	}

}

// Update runs by gobou update hoge/foo
func (ii *InstallInfo) Update(pluginDir, cacheDir, pluginConfigDir string) {
	gurl, err := parseInstallURL(ii.url)
	srcDir := filepath.Join(cacheDir, "src")
	src := getSrcPath(ii.name, srcDir)
	dist := getDistPath(ii.name, pluginDir)
	if err != nil {
		return
	}
	err = pull(gurl, src)
	if err != nil {
		log.Fatalln(err)
		return
	}
	err = build(src, dist)
	if err != nil {
		log.Fatalln(err)
		return
	}
}

// run git clone command
func clone(gurl, srcPath string) error {
	out, err := exec.Command("git", "clone", gurl, srcPath).CombinedOutput()
	log.Println(string(out))
	return err
}

// run git pull command
func pull(gurl, srcPath string) error {
	out, err := exec.Command("git", "-C", srcPath, "pull").CombinedOutput()
	log.Println(string(out))
	return err
}

// run go build command
func build(src, dist string) error {
	pwd, err := filepath.Abs(".")
	if err != nil {
		return err
	}
	relsrc, err := filepath.Rel(pwd, src)
	if err != nil {
		return err
	}
	out, err := exec.Command("go", "build", "-o", dist, relsrc).CombinedOutput()
	log.Println(string(out))
	return err
}

func getSrcPath(name, srcDir string) string {
	return filepath.Join(srcDir, name)
}

func getDistPath(name, pluginDir string) string {
	return filepath.Join(pluginDir, name)
}

// TODO: if url is illeagal, raise error
func parseInstallURL(url string) (string, error) {
	githubURL := "https://github.com/" + url + ".git"
	return githubURL, nil
}

func initConfigFile(file string) error {
	_, e := os.Stat(file)
	if os.IsNotExist(e) {
		return ioutil.WriteFile(file, []byte("{}"), os.ModePerm)
	}
	return nil
}
<commit_msg>install: fix path of config file<commit_after>package main

import (
	"io/ioutil"
	"log"
	"os"
	"os/exec"
	"path/filepath"
)

// InstallInfo is
type InstallInfo struct {
	url  string
	name string
}

// Install runs by gobou install hoge/foo
func (ii *InstallInfo) Install(pluginDir, cacheDir, pluginConfigDir string) {
	gurl, err := parseInstallURL(ii.url)
	srcDir := filepath.Join(cacheDir, "src")
	src := getSrcPath(ii.name, srcDir)
	dist := getDistPath(ii.name, pluginDir)
	if err != nil {
		return
	}
	err = clone(gurl, src)
	if err != nil {
		log.Fatalln(err)
		return
	}
	err = build(src, dist)
	if err != nil {
		log.Fatalln(err)
		return
	}

	err = initConfigFile(filepath.Join(pluginConfigDir, ii.name) + ".json")
	if err != nil {
		log.Fatalln(err)
		return
	}

}

// Update runs by gobou update hoge/foo
func (ii *InstallInfo) Update(pluginDir, cacheDir, pluginConfigDir string) {
	gurl, err := parseInstallURL(ii.url)
	srcDir := filepath.Join(cacheDir, "src")
	src := getSrcPath(ii.name, srcDir)
	dist := getDistPath(ii.name, pluginDir)
	if err != nil {
		return
	}
	err = pull(gurl, src)
	if err != nil {
		log.Fatalln(err)
		return
	}
	err = build(src, dist)
	if err != nil {
		log.Fatalln(err)
		return
	}
}

// run git clone command
func clone(gurl, srcPath string) error {
	out, err := exec.Command("git", "clone", gurl, srcPath).CombinedOutput()
	log.Println(string(out))
	return err
}

// run git pull command
func pull(gurl, srcPath string) error {
	out, err := exec.Command("git", "-C", srcPath, "pull").CombinedOutput()
	log.Println(string(out))
	return err
}

// run go build command
func build(src, dist string) error {
	pwd, err := filepath.Abs(".")
	if err != nil {
		return err
	}
	relsrc, err := filepath.Rel(pwd, src)
	if err != nil {
		return err
	}
	out, err := exec.Command("go", "build", "-o", dist, relsrc).CombinedOutput()
	log.Println(string(out))
	return err
}

func getSrcPath(name, srcDir string) string {
	return filepath.Join(srcDir, name)
}

func getDistPath(name, pluginDir string) string {
	return filepath.Join(pluginDir, name)
}

// TODO: if url is illeagal, raise error
func parseInstallURL(url string) (string, error) {
	githubURL := "https://github.com/" + url + ".git"
	return githubURL, nil
}

func initConfigFile(file string) error {
	_, e := os.Stat(file)
	if os.IsNotExist(e) {
		return ioutil.WriteFile(file, []byte("{}"), os.ModePerm)
	}
	return nil
}
<|endoftext|>


<commit_before>package nextbaby

import (
	iniconf "code.google.com/p/goconf/conf"
	"fmt"
	"github.com/gamelost/bot3server/server"
	"time"
)

type NextBabyService struct {
	server.BotHandlerService
}

func (svc *NextBabyService) NewService(config *iniconf.ConfigFile, publishToIRCChan chan *server.BotResponse) server.BotHandler {
	newSvc := &NextBabyService{}
	newSvc.Config = config
	newSvc.PublishToIRCChan = publishToIRCChan
	return newSvc
}

func (svc *NextBabyService) DispatchRequest(botRequest *server.BotRequest) {

	botResponse := svc.CreateBotResponse(botRequest)
	botResponse.SetSingleLineResponse(durationToDate())
	svc.PublishBotResponse(botResponse)
}

func durationToDate() string {

	nowDate := time.Now()
	loc, _ := time.LoadLocation("America/Los_Angeles")
	weddingDate := time.Date(2015, time.February, 24, 12, 0, 0, 0, loc)

	if nowDate.After(weddingDate) {
		return "Ah mon. It be too late, has da baby popped out yet?!"
	} else {

		duration := weddingDate.Sub(nowDate)
		durationInMinutes := int(duration.Minutes())
		durationDays := durationInMinutes / (24 * 60)
		durationHours := (durationInMinutes - (durationDays * 60 * 24)) / 60
		durationMinutes := durationInMinutes - ((durationDays * 60 * 24) + (durationHours * 60))

		return fmt.Sprintf("sonOfAshburn rises in %d days, %d hours and %d minutes! Have you shopped at Baby-R-Us yet?!", durationDays, durationHours, durationMinutes)
	}
}
<commit_msg>updating for early baby<commit_after>package nextbaby

import (
	iniconf "code.google.com/p/goconf/conf"
	"fmt"
	"github.com/gamelost/bot3server/server"
	"time"
)

type NextBabyService struct {
	server.BotHandlerService
}

func (svc *NextBabyService) NewService(config *iniconf.ConfigFile, publishToIRCChan chan *server.BotResponse) server.BotHandler {
	newSvc := &NextBabyService{}
	newSvc.Config = config
	newSvc.PublishToIRCChan = publishToIRCChan
	return newSvc
}

func (svc *NextBabyService) DispatchRequest(botRequest *server.BotRequest) {

	botResponse := svc.CreateBotResponse(botRequest)
	botResponse.SetSingleLineResponse(durationToDate())
	svc.PublishBotResponse(botResponse)
}

func durationToDate() string {

	nowDate := time.Now()
	loc, _ := time.LoadLocation("America/Los_Angeles")
	weddingDate := time.Date(2015, time.February, 2, 01, 0, 0, 0, loc)

	if nowDate.After(weddingDate) {
		return "Baby done popped out! Congrats ashburn!"
	} else {

		duration := weddingDate.Sub(nowDate)
		durationInMinutes := int(duration.Minutes())
		durationDays := durationInMinutes / (24 * 60)
		durationHours := (durationInMinutes - (durationDays * 60 * 24)) / 60
		durationMinutes := durationInMinutes - ((durationDays * 60 * 24) + (durationHours * 60))

		return fmt.Sprintf("sonOfAshburn rises in %d days, %d hours and %d minutes! Have you shopped at Baby-R-Us yet?!", durationDays, durationHours, durationMinutes)
	}
}
<|endoftext|>


<commit_before>package aws

import (
	"fmt"
	"testing"

	"github.com/hashicorp/terraform/helper/resource"
	"github.com/hashicorp/terraform/terraform"
)

func testAccAWSSecurityHubProductSubscription_basic(t *testing.T) {
	resource.Test(t, resource.TestCase{
		PreCheck:     func() { testAccPreCheck(t) },
		Providers:    testAccProviders,
		CheckDestroy: testAccCheckAWSSecurityHubAccountDestroy,
		Steps: []resource.TestStep{
			{
				Config: testAccAWSSecurityHubProductSubscriptionConfig_basic,
				Check: resource.ComposeTestCheckFunc(
					testAccCheckAWSSecurityHubProductSubscriptionExists("aws_securityhub_product_subscription.example"),
				),
			},
			{
				ResourceName:      "aws_securityhub_product_subscription.example",
				ImportState:       true,
				ImportStateVerify: true,
			},
			{
				// Check Destroy - but only target the specific resource (otherwise Security Hub
				// will be disabled and the destroy check will fail)
				Config: testAccAWSSecurityHubProductSubscriptionConfig_empty,
				Check:  testAccCheckAWSSecurityHubProductSubscriptionDestroy,
			},
		},
	})
}

func testAccCheckAWSSecurityHubProductSubscriptionExists(n string) resource.TestCheckFunc {
	return func(s *terraform.State) error {
		rs, ok := s.RootModule().Resources[n]
		if !ok {
			return fmt.Errorf("Not found: %s", n)
		}

		conn := testAccProvider.Meta().(*AWSClient).securityhubconn

		_, productSubscriptionArn, err := resourceAwsSecurityHubProductSubscriptionParseId(rs.Primary.ID)

		if err != nil {
			return err
		}

		exists, err := resourceAwsSecurityHubProductSubscriptionCheckExists(conn, productSubscriptionArn)

		if err != nil {
			return err
		}

		if !exists {
			return fmt.Errorf("Security Hub product subscription %s not found", rs.Primary.ID)
		}

		return nil
	}
}

func testAccCheckAWSSecurityHubProductSubscriptionDestroy(s *terraform.State) error {
	conn := testAccProvider.Meta().(*AWSClient).securityhubconn

	for _, rs := range s.RootModule().Resources {
		if rs.Type != "aws_securityhub_product_subscription" {
			continue
		}

		_, productSubscriptionArn, err := resourceAwsSecurityHubProductSubscriptionParseId(rs.Primary.ID)

		if err != nil {
			return err
		}

		exists, err := resourceAwsSecurityHubProductSubscriptionCheckExists(conn, productSubscriptionArn)

		if err != nil {
			return err
		}

		if exists {
			return fmt.Errorf("Security Hub product subscription %s still exists", rs.Primary.ID)
		}
	}

	return nil
}

const testAccAWSSecurityHubProductSubscriptionConfig_empty = `
resource "aws_securityhub_account" "example" {}
`

const testAccAWSSecurityHubProductSubscriptionConfig_basic = `
resource "aws_securityhub_account" "example" {}

data "aws_region" "current" {}

resource "aws_securityhub_product_subscription" "example" {
  depends_on  = ["aws_securityhub_account.example"]
  product_arn = "arn:aws:securityhub:${data.aws_region.current.name}:733251395267:product/alertlogic/althreatmanagement"
}
`
<commit_msg>tests/resource/aws_securityhub_product_subscription: Use AWS product subscription instead of third-party<commit_after>package aws

import (
	"fmt"
	"testing"

	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/aws/arn"
	"github.com/aws/aws-sdk-go/service/securityhub"

	"github.com/hashicorp/terraform/helper/resource"
	"github.com/hashicorp/terraform/terraform"
)

func testAccAWSSecurityHubProductSubscription_basic(t *testing.T) {
	resource.Test(t, resource.TestCase{
		PreCheck:     func() { testAccPreCheck(t) },
		Providers:    testAccProviders,
		CheckDestroy: testAccCheckAWSSecurityHubAccountDestroy,
		Steps: []resource.TestStep{
			{
				// We would like to use an AWS product subscription, but they are
				// all automatically subscribed when enabling Security Hub.
				// This configuration will enable Security Hub, then in a later PreConfig,
				// we will disable an AWS product subscription so we can test (re-)enabling it.
				Config: testAccAWSSecurityHubProductSubscriptionConfig_empty,
				Check:  testAccCheckAWSSecurityHubAccountExists("aws_securityhub_account.example"),
			},
			{
				// AWS product subscriptions happen automatically when enabling Security Hub.
				// Here we attempt to remove one so we can attempt to (re-)enable it.
				PreConfig: func() {
					conn := testAccProvider.Meta().(*AWSClient).securityhubconn
					productSubscriptionARN := arn.ARN{
						AccountID: testAccGetAccountID(),
						Partition: testAccGetPartition(),
						Region:    testAccGetRegion(),
						Resource:  "product-subscription/aws/guardduty",
						Service:   "securityhub",
					}.String()

					input := &securityhub.DisableImportFindingsForProductInput{
						ProductSubscriptionArn: aws.String(productSubscriptionARN),
					}

					_, err := conn.DisableImportFindingsForProduct(input)
					if err != nil {
						t.Fatalf("error disabling Security Hub Product Subscription for GuardDuty: %s", err)
					}
				},
				Config: testAccAWSSecurityHubProductSubscriptionConfig_basic,
				Check: resource.ComposeTestCheckFunc(
					testAccCheckAWSSecurityHubProductSubscriptionExists("aws_securityhub_product_subscription.example"),
				),
			},
			{
				ResourceName:      "aws_securityhub_product_subscription.example",
				ImportState:       true,
				ImportStateVerify: true,
			},
			{
				// Check Destroy - but only target the specific resource (otherwise Security Hub
				// will be disabled and the destroy check will fail)
				Config: testAccAWSSecurityHubProductSubscriptionConfig_empty,
				Check:  testAccCheckAWSSecurityHubProductSubscriptionDestroy,
			},
		},
	})
}

func testAccCheckAWSSecurityHubProductSubscriptionExists(n string) resource.TestCheckFunc {
	return func(s *terraform.State) error {
		rs, ok := s.RootModule().Resources[n]
		if !ok {
			return fmt.Errorf("Not found: %s", n)
		}

		conn := testAccProvider.Meta().(*AWSClient).securityhubconn

		_, productSubscriptionArn, err := resourceAwsSecurityHubProductSubscriptionParseId(rs.Primary.ID)

		if err != nil {
			return err
		}

		exists, err := resourceAwsSecurityHubProductSubscriptionCheckExists(conn, productSubscriptionArn)

		if err != nil {
			return err
		}

		if !exists {
			return fmt.Errorf("Security Hub product subscription %s not found", rs.Primary.ID)
		}

		return nil
	}
}

func testAccCheckAWSSecurityHubProductSubscriptionDestroy(s *terraform.State) error {
	conn := testAccProvider.Meta().(*AWSClient).securityhubconn

	for _, rs := range s.RootModule().Resources {
		if rs.Type != "aws_securityhub_product_subscription" {
			continue
		}

		_, productSubscriptionArn, err := resourceAwsSecurityHubProductSubscriptionParseId(rs.Primary.ID)

		if err != nil {
			return err
		}

		exists, err := resourceAwsSecurityHubProductSubscriptionCheckExists(conn, productSubscriptionArn)

		if err != nil {
			return err
		}

		if exists {
			return fmt.Errorf("Security Hub product subscription %s still exists", rs.Primary.ID)
		}
	}

	return nil
}

const testAccAWSSecurityHubProductSubscriptionConfig_empty = `
resource "aws_securityhub_account" "example" {}
`

const testAccAWSSecurityHubProductSubscriptionConfig_basic = `
resource "aws_securityhub_account" "example" {}

data "aws_region" "current" {}

resource "aws_securityhub_product_subscription" "example" {
  depends_on  = ["aws_securityhub_account.example"]
  product_arn = "arn:aws:securityhub:${data.aws_region.current.name}::product/aws/guardduty"
}
`
<|endoftext|>


<commit_before>package main

import (
	"encoding/json"
	"fmt"
	"github.com/siesta/neo4j"
	"github.com/streadway/amqp"
	"io/ioutil"
	oldNeo "koding/databases/neo4j"
	"koding/db/mongodb"
	"koding/tools/amqputil"
	"koding/tools/config"
	"labix.org/v2/mgo"
	"log"
	"net/http"
	"strconv"
	"strings"
	"time"
)

type strToInf map[string]interface{}

var (
	GRAPH_URL           = config.Current.Neo4j.Write + ":" + strconv.Itoa(config.Current.Neo4j.Port)
	EXCHANGENAME        = "graphFeederExchange"
	MAX_ITERATION_COUNT = 50
	GUEST_GROUP_ID      = "51f41f195f07655e560001c1"
	SLEEPING_TIME       = 100 * time.Millisecond
)

func main() {
	log.Println("Sync worker started")

	amqpChannel := connectToRabbitMQ()
	log.Println("Connected to Rabbit")

	err := mongodb.Run("relationships", createQuery(amqpChannel))
	if err != nil {
		fmt.Println("Error >>>", err)
	}
}

func createQuery(amqpChannel *amqp.Channel) func(coll *mgo.Collection) error {

	return func(coll *mgo.Collection) error {
		filter := strToInf{
			"targetName": strToInf{"$nin": oldNeo.NotAllowedNames},
			"sourceName": strToInf{"$nin": oldNeo.NotAllowedNames},
		}
		query := coll.Find(filter)

		totalCount, err := query.Count()
		if err != nil {
			log.Println("Err while getting count, exiting", err)
			return err
		}

		skip := config.Skip
		// this is a starting point
		index := skip
		// this is the item count to be processed
		limit := config.Count
		// this will be the ending point
		count := index + limit

		var result oldNeo.Relationship

		iteration := 0
		for {
			// if we reach to the end of the all collection, exit
			if index >= totalCount {
				log.Println("All items are processed, exiting")
				break
			}

			// this is the max re-iterating count
			if iteration == MAX_ITERATION_COUNT {
				break
			}

			// if we processed all items then exit
			if index == count {
				break
			}

			iter := query.Skip(index).Limit(count - index).Iter()
			for iter.Next(&result) {
				time.Sleep(SLEEPING_TIME)

				if relationshipNeedsToBeSynced(result) {
					createRelationship(result, amqpChannel)
				}

				index++
				log.Println(index)
			}

			if err := iter.Close(); err != nil {
				log.Println(err)
			}

			if iter.Timeout() {
				continue
			}

			log.Printf("iter existed, starting over from %v  -- %v  item(s) are processsed on this iter", index+1, index-skip)
			iteration++
		}

		if iteration == MAX_ITERATION_COUNT {
			log.Printf("Max iteration count %v reached, exiting", iteration)
		}
		log.Printf("Synced %v entries on this process", index-skip)

		return nil
	}
}

func connectToRabbitMQ() *amqp.Channel {
	conn := amqputil.CreateConnection("syncWorker")
	amqpChannel, err := conn.Channel()
	if err != nil {
		panic(err)
	}
	return amqpChannel
}

func createRelationship(rel oldNeo.Relationship, amqpChannel *amqp.Channel) {
	data := make([]strToInf, 1)
	data[0] = strToInf{
		"_id":        rel.Id,
		"sourceId":   rel.SourceId,
		"sourceName": rel.SourceName,
		"targetId":   rel.TargetId,
		"targetName": rel.TargetName,
		"as":         rel.As,
	}

	eventData := strToInf{"event": "RelationshipSaved", "payload": data}

	neoMessage, err := json.Marshal(eventData)
	if err != nil {
		log.Println("unmarshall error")
		return
	}

	amqpChannel.Publish(
		EXCHANGENAME, // exchange name
		"",           // key
		false,        // mandatory
		false,        // immediate
		amqp.Publishing{
			Body: neoMessage,
		},
	)
}

func relationshipNeedsToBeSynced(result oldNeo.Relationship) bool {
	if result.SourceId.Hex() == GUEST_GROUP_ID || result.TargetId.Hex() == GUEST_GROUP_ID {
		return false
	}

	exists, sourceId := checkNodeExists(result.SourceId.Hex())
	if exists != true {
		logError(result, "No source node")
		return true
	}

	exists, targetId := checkNodeExists(result.TargetId.Hex())
	if exists != true {
		logError(result, "No target node")
		return true
	}

	// flip JGroup relationships since they take a long time to check
	var flipped = false
	if result.SourceName == "JGroup" {
		flipped = true

		tempId := sourceId
		sourceId = targetId
		targetId = tempId
	}

	exists = checkRelationshipExists(sourceId, targetId, result.As, flipped)
	if exists != true {
		logError(result, "No relationship")
		return true
	}

	// everything is fine
	return false
}

func getAndParse(url string) ([]byte, error) {
	resp, err := http.Get(url)
	if err != nil {
		return nil, err
	}

	defer resp.Body.Close()
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return nil, err
	}

	return body, nil
}

func checkRelationshipExists(sourceId, targetId, relType string, flipped bool) bool {
	url := fmt.Sprintf("%v/db/data/node/%v/relationships/all/%v", GRAPH_URL, sourceId, relType)

	body, err := getAndParse(url)
	if err != nil {
		return false
	}

	relResponse := make([]neo4j.RelationshipResponse, 1)
	err = json.Unmarshal(body, &relResponse)
	if err != nil {
		return false
	}

	var numberofRelsFound = 0
	var relIds []string

	for _, rl := range relResponse {
		var checkPos string
		if flipped {
			checkPos = rl.Start
		} else {
			checkPos = rl.End
		}

		id := strings.SplitAfter(checkPos, GRAPH_URL+"/db/data/node/")[1]
		if targetId == id {
			numberofRelsFound++
			relIds = append(relIds, getRelIdFromUrl(rl.Self))
		}
	}

	switch numberofRelsFound {
	case 0:
		return false
	case 1:
		return true
	default:
		log.Printf("multiple '%v' rel %v", relType, relIds)

		if relType == "member" || relType == "creator" || relType == "author" {
			deleteDuplicateRel(relIds[1:])
			log.Printf("deleted multiple '%v' rel", relType)
		}
	}

	return true
}

func deleteDuplicateRel(relIds []string) {
	for _, id := range relIds {
		rel := neo4j.Relationship{Id: id}

		neo4jConnection := neo4j.Connect(GRAPH_URL)
		batch := neo4jConnection.NewBatch().Delete(&rel)

		_, err := batch.Execute()
		if err != nil {
			log.Println("err deleting rel", err)
		}
	}
}

func checkNodeExists(id string) (bool, string) {
	url := fmt.Sprintf("%v/db/data/index/node/koding/id/%v", GRAPH_URL, id)
	body, err := getAndParse(url)
	if err != nil {
		return false, ""
	}

	nodeResponse := make([]neo4j.NodeResponse, 1)
	err = json.Unmarshal(body, &nodeResponse)
	if err != nil {
		return false, ""
	}

	if len(nodeResponse) < 1 {
		return false, ""
	}

	node := nodeResponse[0]
	idd := getNodeIdFromUrl(node.Self)

	nodeId := string(idd)
	if nodeId == "" {
		return false, ""
	}

	return true, nodeId
}

func getNodeIdFromUrl(nodeSelf string) string {
	return strings.SplitAfter(nodeSelf, GRAPH_URL+"/db/data/node/")[1]
}

func getRelIdFromUrl(nodeSelf string) string {
	return strings.SplitAfter(nodeSelf, GRAPH_URL+"/db/data/relationship/")[1]
}

func logError(result oldNeo.Relationship, errMsg string) {
	log.Printf("id: %v, type: %v, source: {%v %v} target: {%v %v}; err: %v", result.SourceId.Hex(), result.As, result.SourceId.Hex(), result.SourceName, result.TargetId.Hex(), result.TargetName, errMsg)
}
<commit_msg>sync: delete rels in a batch request<commit_after>package main

import (
	"encoding/json"
	"fmt"
	"github.com/siesta/neo4j"
	"github.com/streadway/amqp"
	"io/ioutil"
	oldNeo "koding/databases/neo4j"
	"koding/db/mongodb"
	"koding/tools/amqputil"
	"koding/tools/config"
	"labix.org/v2/mgo"
	"log"
	"net/http"
	"strconv"
	"strings"
	"time"
)

type strToInf map[string]interface{}

var (
	GRAPH_URL           = config.Current.Neo4j.Write + ":" + strconv.Itoa(config.Current.Neo4j.Port)
	EXCHANGENAME        = "graphFeederExchange"
	MAX_ITERATION_COUNT = 50
	GUEST_GROUP_ID      = "51f41f195f07655e560001c1"
	SLEEPING_TIME       = 100 * time.Millisecond
)

func main() {
	log.Println("Sync worker started")

	amqpChannel := connectToRabbitMQ()
	log.Println("Connected to Rabbit")

	err := mongodb.Run("relationships", createQuery(amqpChannel))
	if err != nil {
		fmt.Println("Error >>>", err)
	}
}

func createQuery(amqpChannel *amqp.Channel) func(coll *mgo.Collection) error {

	return func(coll *mgo.Collection) error {
		filter := strToInf{
			"targetName": strToInf{"$nin": oldNeo.NotAllowedNames},
			"sourceName": strToInf{"$nin": oldNeo.NotAllowedNames},
		}
		query := coll.Find(filter)

		totalCount, err := query.Count()
		if err != nil {
			log.Println("Err while getting count, exiting", err)
			return err
		}

		skip := config.Skip
		// this is a starting point
		index := skip
		// this is the item count to be processed
		limit := config.Count
		// this will be the ending point
		count := index + limit

		var result oldNeo.Relationship

		iteration := 0
		for {
			// if we reach to the end of the all collection, exit
			if index >= totalCount {
				log.Println("All items are processed, exiting")
				break
			}

			// this is the max re-iterating count
			if iteration == MAX_ITERATION_COUNT {
				break
			}

			// if we processed all items then exit
			if index == count {
				break
			}

			iter := query.Skip(index).Limit(count - index).Iter()
			for iter.Next(&result) {
				time.Sleep(SLEEPING_TIME)

				if relationshipNeedsToBeSynced(result) {
					createRelationship(result, amqpChannel)
				}

				index++
				log.Println(index)
			}

			if err := iter.Close(); err != nil {
				log.Println(err)
			}

			if iter.Timeout() {
				continue
			}

			log.Printf("iter existed, starting over from %v  -- %v  item(s) are processsed on this iter", index+1, index-skip)
			iteration++
		}

		if iteration == MAX_ITERATION_COUNT {
			log.Printf("Max iteration count %v reached, exiting", iteration)
		}
		log.Printf("Synced %v entries on this process", index-skip)

		return nil
	}
}

func connectToRabbitMQ() *amqp.Channel {
	conn := amqputil.CreateConnection("syncWorker")
	amqpChannel, err := conn.Channel()
	if err != nil {
		panic(err)
	}
	return amqpChannel
}

func createRelationship(rel oldNeo.Relationship, amqpChannel *amqp.Channel) {
	data := make([]strToInf, 1)
	data[0] = strToInf{
		"_id":        rel.Id,
		"sourceId":   rel.SourceId,
		"sourceName": rel.SourceName,
		"targetId":   rel.TargetId,
		"targetName": rel.TargetName,
		"as":         rel.As,
	}

	eventData := strToInf{"event": "RelationshipSaved", "payload": data}

	neoMessage, err := json.Marshal(eventData)
	if err != nil {
		log.Println("unmarshall error")
		return
	}

	amqpChannel.Publish(
		EXCHANGENAME, // exchange name
		"",           // key
		false,        // mandatory
		false,        // immediate
		amqp.Publishing{
			Body: neoMessage,
		},
	)
}

func relationshipNeedsToBeSynced(result oldNeo.Relationship) bool {
	if result.SourceId.Hex() == GUEST_GROUP_ID || result.TargetId.Hex() == GUEST_GROUP_ID {
		return false
	}

	exists, sourceId := checkNodeExists(result.SourceId.Hex())
	if exists != true {
		logError(result, "No source node")
		return true
	}

	exists, targetId := checkNodeExists(result.TargetId.Hex())
	if exists != true {
		logError(result, "No target node")
		return true
	}

	// flip JGroup relationships since they take a long time to check
	var flipped = false
	if result.SourceName == "JGroup" {
		flipped = true

		tempId := sourceId
		sourceId = targetId
		targetId = tempId
	}

	exists = checkRelationshipExists(sourceId, targetId, result.As, flipped)
	if exists != true {
		logError(result, "No relationship")
		return true
	}

	// everything is fine
	return false
}

func getAndParse(url string) ([]byte, error) {
	resp, err := http.Get(url)
	if err != nil {
		return nil, err
	}

	defer resp.Body.Close()
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return nil, err
	}

	return body, nil
}

func checkRelationshipExists(sourceId, targetId, relType string, flipped bool) bool {
	url := fmt.Sprintf("%v/db/data/node/%v/relationships/all/%v", GRAPH_URL, sourceId, relType)

	body, err := getAndParse(url)
	if err != nil {
		return false
	}

	relResponse := make([]neo4j.RelationshipResponse, 1)
	err = json.Unmarshal(body, &relResponse)
	if err != nil {
		return false
	}

	var numberofRelsFound = 0
	var relIds []string

	for _, rl := range relResponse {
		var checkPos string
		if flipped {
			checkPos = rl.Start
		} else {
			checkPos = rl.End
		}

		id := strings.SplitAfter(checkPos, GRAPH_URL+"/db/data/node/")[1]
		if targetId == id {
			numberofRelsFound++
			relIds = append(relIds, getRelIdFromUrl(rl.Self))
		}
	}

	switch numberofRelsFound {
	case 0:
		return false
	case 1:
		return true
	default:
		log.Printf("multiple '%v' rel %v", relType, relIds)

		if relType == "member" || relType == "creator" || relType == "author" {
			deleteDuplicateRel(relIds[1:])
			log.Printf("deleted multiple '%v' rel", relType)
		}
	}

	return true
}

func deleteDuplicateRel(relIds []string) {
	neo4jConnection := neo4j.Connect(GRAPH_URL)
	batch := neo4jConnection.NewBatch()

	for _, id := range relIds {
		rel := neo4j.Relationship{Id: id}
		batch.Delete(&rel)
	}

	_, err := batch.Execute()
	if err != nil {
		log.Println("err deleting rel", err)
	}
}

func checkNodeExists(id string) (bool, string) {
	url := fmt.Sprintf("%v/db/data/index/node/koding/id/%v", GRAPH_URL, id)
	body, err := getAndParse(url)
	if err != nil {
		return false, ""
	}

	nodeResponse := make([]neo4j.NodeResponse, 1)
	err = json.Unmarshal(body, &nodeResponse)
	if err != nil {
		return false, ""
	}

	if len(nodeResponse) < 1 {
		return false, ""
	}

	node := nodeResponse[0]
	idd := getNodeIdFromUrl(node.Self)

	nodeId := string(idd)
	if nodeId == "" {
		return false, ""
	}

	return true, nodeId
}

func getNodeIdFromUrl(nodeSelf string) string {
	return strings.SplitAfter(nodeSelf, GRAPH_URL+"/db/data/node/")[1]
}

func getRelIdFromUrl(nodeSelf string) string {
	return strings.SplitAfter(nodeSelf, GRAPH_URL+"/db/data/relationship/")[1]
}

func logError(result oldNeo.Relationship, errMsg string) {
	log.Printf("id: %v, type: %v, source: {%v %v} target: {%v %v}; err: %v", result.SourceId.Hex(), result.As, result.SourceId.Hex(), result.SourceName, result.TargetId.Hex(), result.TargetName, errMsg)
}
<|endoftext|>


<commit_before>/*
Copyright 2020 The Kubernetes Authors All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package machine

import (
	"runtime"

	"github.com/pkg/errors"
	"k8s.io/minikube/pkg/drivers/kic/oci"
	"k8s.io/minikube/pkg/minikube/out"
	"k8s.io/minikube/pkg/minikube/style"
)

// MaybeDisplayAdvice will provide advice without exiting, so minikube has a chance to try the failover
func MaybeDisplayAdvice(err error, driver string) {
	if errors.Is(err, oci.ErrDaemonInfo) {
		out.ErrLn("")
		out.ErrT(style.Conflict, "{{.driver_name}} couldn't proceed because {{.driver_name}} service is not healthy.", out.V{"driver_name": driver})
	}

	if errors.Is(err, oci.ErrExitedUnexpectedly) {
		out.ErrLn("")
		out.ErrT(style.Conflict, "The minikube {{.driver_name}} container exited unexpectedly.", out.V{"driver_name": driver})
	}

	if errors.Is(err, oci.ErrExitedUnexpectedly) || errors.Is(err, oci.ErrDaemonInfo) {
		out.T(style.Tip, "If you are still interested to make {{.driver_name}} driver work. The following suggestions might help you get passed this issue:", out.V{"driver_name": driver})
		out.T(style.Empty, `
	- Prune unused {{.driver_name}} images, volumes and abandoned containers.`, out.V{"driver_name": driver})
		out.T(style.Empty, `
	- Restart your {{.driver_name}} service`, out.V{"driver_name": driver})
		if runtime.GOOS != "linux" {
			out.T(style.Empty, `
	- Ensure your {{.driver_name}} daemon has access to enough CPU/memory resources. `, out.V{"driver_name": driver})
			if runtime.GOOS == "darwin" && driver == oci.Docker {
				out.T(style.Empty, `
	- Docs https://docs.docker.com/docker-for-mac/#resources`, out.V{"driver_name": driver})
			}
			if runtime.GOOS == "windows" && driver == oci.Docker {
				out.T(style.Empty, `
	- Docs https://docs.docker.com/docker-for-windows/#resources`, out.V{"driver_name": driver})
			}
		}
		out.T(style.Empty, `
	- Delete and recreate minikube cluster
		minikube delete
		minikube start --driver={{.driver_name}}`, out.V{"driver_name": driver})
		// TODO #8348: maybe advice user if to set the --force-systemd https://github.com/kubernetes/minikube/issues/8348
	}
}
<commit_msg>Add prune docker images instructions<commit_after>/*
Copyright 2020 The Kubernetes Authors All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package machine

import (
	"runtime"

	"github.com/pkg/errors"
	"k8s.io/minikube/pkg/drivers/kic/oci"
	"k8s.io/minikube/pkg/minikube/out"
	"k8s.io/minikube/pkg/minikube/style"
)

// MaybeDisplayAdvice will provide advice without exiting, so minikube has a chance to try the failover
func MaybeDisplayAdvice(err error, driver string) {
	if errors.Is(err, oci.ErrDaemonInfo) {
		out.ErrLn("")
		out.ErrT(style.Conflict, "{{.driver_name}} couldn't proceed because {{.driver_name}} service is not healthy.", out.V{"driver_name": driver})
	}

	if errors.Is(err, oci.ErrExitedUnexpectedly) {
		out.ErrLn("")
		out.ErrT(style.Conflict, "The minikube {{.driver_name}} container exited unexpectedly.", out.V{"driver_name": driver})
	}

	if errors.Is(err, oci.ErrExitedUnexpectedly) || errors.Is(err, oci.ErrDaemonInfo) {
		out.T(style.Tip, "If you are still interested to make {{.driver_name}} driver work. The following suggestions might help you get passed this issue:", out.V{"driver_name": driver})
		out.T(style.Empty, `
	- Prune unused {{.driver_name}} images, volumes, networks and abandoned containers.
		docker system prune --volumes`, out.V{"driver_name": driver})
		out.T(style.Empty, `
	- Restart your {{.driver_name}} service`, out.V{"driver_name": driver})
		if runtime.GOOS != "linux" {
			out.T(style.Empty, `
	- Ensure your {{.driver_name}} daemon has access to enough CPU/memory resources. `, out.V{"driver_name": driver})
			if runtime.GOOS == "darwin" && driver == oci.Docker {
				out.T(style.Empty, `
	- Docs https://docs.docker.com/docker-for-mac/#resources`, out.V{"driver_name": driver})
			}
			if runtime.GOOS == "windows" && driver == oci.Docker {
				out.T(style.Empty, `
	- Docs https://docs.docker.com/docker-for-windows/#resources`, out.V{"driver_name": driver})
			}
		}
		out.T(style.Empty, `
	- Delete and recreate minikube cluster
		minikube delete
		minikube start --driver={{.driver_name}}`, out.V{"driver_name": driver})
		// TODO #8348: maybe advice user if to set the --force-systemd https://github.com/kubernetes/minikube/issues/8348
	}
}
<|endoftext|>


<commit_before>// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build darwin dragonfly freebsd linux nacl netbsd openbsd solaris

// Read system port mappings from /etc/services

package net

import "sync"

// services contains minimal mappings between services names and port
// numbers for platforms that don't have a complete list of port numbers
// (some Solaris distros).
var services = map[string]map[string]int{
	"tcp": {"http": 80},
}
var servicesError error
var onceReadServices sync.Once

func readServices() {
	var file *file
	if file, servicesError = open("/etc/services"); servicesError != nil {
		return
	}
	for line, ok := file.readLine(); ok; line, ok = file.readLine() {
		// "http 80/tcp www www-http # World Wide Web HTTP"
		if i := byteIndex(line, '#'); i >= 0 {
			line = line[0:i]
		}
		f := getFields(line)
		if len(f) < 2 {
			continue
		}
		portnet := f[1] // "tcp/80"
		port, j, ok := dtoi(portnet, 0)
		if !ok || port <= 0 || j >= len(portnet) || portnet[j] != '/' {
			continue
		}
		netw := portnet[j+1:] // "tcp"
		m, ok1 := services[netw]
		if !ok1 {
			m = make(map[string]int)
			services[netw] = m
		}
		for i := 0; i < len(f); i++ {
			if i != 1 { // f[1] was port/net
				m[f[i]] = port
			}
		}
	}
	file.close()
}

// goLookupPort is the native Go implementation of LookupPort.
func goLookupPort(network, service string) (port int, err error) {
	onceReadServices.Do(readServices)

	switch network {
	case "tcp4", "tcp6":
		network = "tcp"
	case "udp4", "udp6":
		network = "udp"
	}

	if m, ok := services[network]; ok {
		if port, ok = m[service]; ok {
			return
		}
	}
	return 0, &AddrError{"unknown port", network + "/" + service}
}
<commit_msg>net: fix confusing typo in comment<commit_after>// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build darwin dragonfly freebsd linux nacl netbsd openbsd solaris

// Read system port mappings from /etc/services

package net

import "sync"

// services contains minimal mappings between services names and port
// numbers for platforms that don't have a complete list of port numbers
// (some Solaris distros).
var services = map[string]map[string]int{
	"tcp": {"http": 80},
}
var servicesError error
var onceReadServices sync.Once

func readServices() {
	var file *file
	if file, servicesError = open("/etc/services"); servicesError != nil {
		return
	}
	for line, ok := file.readLine(); ok; line, ok = file.readLine() {
		// "http 80/tcp www www-http # World Wide Web HTTP"
		if i := byteIndex(line, '#'); i >= 0 {
			line = line[0:i]
		}
		f := getFields(line)
		if len(f) < 2 {
			continue
		}
		portnet := f[1] // "80/tcp"
		port, j, ok := dtoi(portnet, 0)
		if !ok || port <= 0 || j >= len(portnet) || portnet[j] != '/' {
			continue
		}
		netw := portnet[j+1:] // "tcp"
		m, ok1 := services[netw]
		if !ok1 {
			m = make(map[string]int)
			services[netw] = m
		}
		for i := 0; i < len(f); i++ {
			if i != 1 { // f[1] was port/net
				m[f[i]] = port
			}
		}
	}
	file.close()
}

// goLookupPort is the native Go implementation of LookupPort.
func goLookupPort(network, service string) (port int, err error) {
	onceReadServices.Do(readServices)

	switch network {
	case "tcp4", "tcp6":
		network = "tcp"
	case "udp4", "udp6":
		network = "udp"
	}

	if m, ok := services[network]; ok {
		if port, ok = m[service]; ok {
			return
		}
	}
	return 0, &AddrError{"unknown port", network + "/" + service}
}
<|endoftext|>


<commit_before>package lookup

import (
	"bufio"
	"fmt"
	"os"
	"strings"

	"github.com/Sirupsen/logrus"
	"github.com/docker/libcompose/project"
)

type FileEnvLookup struct {
	parent    project.EnvironmentLookup
	variables map[string]string
}

func NewFileEnvLookup(file string, parent project.EnvironmentLookup) (*FileEnvLookup, error) {
	variables := map[string]string{}

	if file != "" {
		f, err := os.Open(file)
		if err != nil {
			return nil, err
		}
		scanner := bufio.NewScanner(f)
		for scanner.Scan() {
			t := scanner.Text()
			parts := strings.SplitN(t, "=", 2)
			if len(parts) == 1 {
				variables[parts[0]] = ""
			} else {
				variables[parts[0]] = parts[1]
			}
		}

		if scanner.Err() != nil {
			return nil, scanner.Err()
		}
	}

	logrus.Debugf("Environemnt Context from file %s: %v", file, variables)
	return &FileEnvLookup{
		parent:    parent,
		variables: variables,
	}, nil
}

func (f *FileEnvLookup) Lookup(key, serviceName string, config *project.ServiceConfig) []string {
	if v, ok := f.variables[key]; ok {
		return []string{fmt.Sprintf("%s=%s", key, v)}
	}

	if f.parent == nil {
		return nil
	}

	return f.parent.Lookup(key, serviceName, config)
}
<commit_msg>Typo in debug message<commit_after>package lookup

import (
	"bufio"
	"fmt"
	"os"
	"strings"

	"github.com/Sirupsen/logrus"
	"github.com/docker/libcompose/project"
)

type FileEnvLookup struct {
	parent    project.EnvironmentLookup
	variables map[string]string
}

func NewFileEnvLookup(file string, parent project.EnvironmentLookup) (*FileEnvLookup, error) {
	variables := map[string]string{}

	if file != "" {
		f, err := os.Open(file)
		if err != nil {
			return nil, err
		}
		scanner := bufio.NewScanner(f)
		for scanner.Scan() {
			t := scanner.Text()
			parts := strings.SplitN(t, "=", 2)
			if len(parts) == 1 {
				variables[parts[0]] = ""
			} else {
				variables[parts[0]] = parts[1]
			}
		}

		if scanner.Err() != nil {
			return nil, scanner.Err()
		}
	}

	logrus.Debugf("Environment Context from file %s: %v", file, variables)
	return &FileEnvLookup{
		parent:    parent,
		variables: variables,
	}, nil
}

func (f *FileEnvLookup) Lookup(key, serviceName string, config *project.ServiceConfig) []string {
	if v, ok := f.variables[key]; ok {
		return []string{fmt.Sprintf("%s=%s", key, v)}
	}

	if f.parent == nil {
		return nil
	}

	return f.parent.Lookup(key, serviceName, config)
}
<|endoftext|>


<commit_before>// Copyright (C) 2019 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 adreno

import (
	"context"
	"fmt"

	"github.com/google/gapid/core/log"
	"github.com/google/gapid/gapis/perfetto"
	"github.com/google/gapid/gapis/trace/android/validate"
)

const (
	renderStageSlicesQuery = "" +
		"select name, depth, parent_stack_id " +
		"from gpu_slice " +
		"where track_id = %v " +
		"order by slice_id"
)

var (
	// All counters must be inside this array.
	counters = []validate.GpuCounter{
		{1, "Clocks / Second", validate.And(validate.IsNumber, validate.CheckLargerThanZero)},
		{3, "GPU % Utilization", validate.And(validate.IsNumber, validate.CheckLargerThanZero)},
		{21, "% Shaders Busy", validate.And(validate.IsNumber, validate.CheckLargerThanZero)},
		{26, "Fragment ALU Instructions / Sec (Full)", validate.And(validate.IsNumber, validate.CheckLargerThanZero)},
		{30, "Textures / Vertex", validate.And(validate.IsNumber, validate.CheckEqualTo(0.0))},
		{31, "Textures / Fragment", validate.And(validate.IsNumber, validate.CheckApproximateTo(1.0, 0.01))},
		{37, "% Time Shading Fragments", validate.And(validate.IsNumber, validate.CheckLargerThanZero)},
		{38, "% Time Shading Vertices", validate.And(validate.IsNumber, validate.CheckLargerThanZero)},
		{39, "% Time Compute", validate.And(validate.IsNumber, validate.CheckEqualTo(0.0))},
	}
)

type AdrenoValidator struct {
}

func (v *AdrenoValidator) validateRenderStage(ctx context.Context, processor *perfetto.Processor) error {
	tIds, err := validate.GetRenderStageTrackIDs(ctx, processor)
	if err != nil {
		return err
	}
	for _, tId := range tIds {
		queryResult, err := processor.Query(fmt.Sprintf(renderStageSlicesQuery, tId))
		if err != nil || queryResult.GetNumRecords() <= 0 {
			return log.Errf(ctx, err, "Failed to query with %v", fmt.Sprintf(renderStageSlicesQuery, tId))
		}
		columns := queryResult.GetColumns()
		names := columns[0].GetStringValues()

		// Skip slices until we hit the first 'Surface' slice.
		skipNum := -1
		hasSurfaceSlice := false
		hasRenderSlice := false
		for i, name := range names {
			if name == "Surface" {
				hasSurfaceSlice = true
				if skipNum == -1 {
					skipNum = i
				}
			}
			if name == "Render" {
				hasRenderSlice = true
			}
		}
		if !hasSurfaceSlice {
			return log.Errf(ctx, err, "Render stage verification failed: No Surface slice found")
		}
		if !hasRenderSlice {
			return log.Errf(ctx, err, "Render stage verification failed: No Render slice found")
		}
		depths := columns[1].GetLongValues()
		parentStackId := columns[2].GetLongValues()

		for i := skipNum; i < len(names); i++ {
			// Surface slice must be the top level slice, hence its depth is 0 and
			// it has no parent stack id.
			// Render slice must be a non-top-level slice, hence its depth must not be 0
			// and it must have a parent stack id.
			if names[i] == "Surface" {
				if depths[i] != 0 || parentStackId[i] != 0 {
					return log.Errf(ctx, err, "Render stage verification failed on Surface slice")
				}
			} else if names[i] == "Render" {
				if depths[i] <= 0 || parentStackId[i] <= 0 {
					return log.Errf(ctx, err, "Render stage verification failed on Render slice")
				}
			}
		}
	}
	return nil
}

func (v *AdrenoValidator) Validate(ctx context.Context, processor *perfetto.Processor) error {
	if err := validate.ValidateGpuCounters(ctx, processor, v.GetCounters()); err != nil {
		return err
	}
	if err := v.validateRenderStage(ctx, processor); err != nil {
		return err
	}

	return nil
}

func (v *AdrenoValidator) GetCounters() []validate.GpuCounter {
	return counters
}
<commit_msg>Update device validation. (#3583)<commit_after>// Copyright (C) 2019 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 adreno

import (
	"context"
	"fmt"
	"strings"

	"github.com/google/gapid/core/log"
	"github.com/google/gapid/gapis/perfetto"
	"github.com/google/gapid/gapis/trace/android/validate"
)

const (
	renderStageSlicesQuery = "" +
		"select name, depth, parent_stack_id " +
		"from gpu_slice " +
		"where track_id = %v " +
		"order by slice_id"
)

var (
	// All counters must be inside this array.
	counters = []validate.GpuCounter{
		{1, "Clocks / Second", validate.And(validate.IsNumber, validate.CheckLargerThanZero)},
		{3, "GPU % Utilization", validate.And(validate.IsNumber, validate.CheckLargerThanZero)},
		{21, "% Shaders Busy", validate.And(validate.IsNumber, validate.CheckLargerThanZero)},
		{26, "Fragment ALU Instructions / Sec (Full)", validate.And(validate.IsNumber, validate.CheckLargerThanZero)},
		{30, "Textures / Vertex", validate.And(validate.IsNumber, validate.CheckEqualTo(0.0))},
		{31, "Textures / Fragment", validate.And(validate.IsNumber, validate.CheckApproximateTo(1.0, 0.1))},
		{37, "% Time Shading Fragments", validate.And(validate.IsNumber, validate.CheckLargerThanZero)},
		{38, "% Time Shading Vertices", validate.And(validate.IsNumber, validate.CheckLargerThanZero)},
		{39, "% Time Compute", validate.And(validate.IsNumber, validate.CheckEqualTo(0.0))},
	}
)

type AdrenoValidator struct {
}

func (v *AdrenoValidator) validateRenderStage(ctx context.Context, processor *perfetto.Processor) error {
	tIds, err := validate.GetRenderStageTrackIDs(ctx, processor)
	if err != nil {
		return err
	}
	for _, tId := range tIds {
		queryResult, err := processor.Query(fmt.Sprintf(renderStageSlicesQuery, tId))
		if err != nil || queryResult.GetNumRecords() <= 0 {
			return log.Errf(ctx, err, "Failed to query with %v", fmt.Sprintf(renderStageSlicesQuery, tId))
		}
		columns := queryResult.GetColumns()
		names := columns[0].GetStringValues()

		// Skip slices until we hit the first 'Surface' slice.
		skipNum := -1
		hasSurfaceSlice := false
		hasRenderSlice := false
		for i, name := range names {
			if strings.Contains(name, "Surface") {
				hasSurfaceSlice = true
				if skipNum == -1 {
					skipNum = i
				}
			}
			if strings.Contains(name, "Render") {
				hasRenderSlice = true
			}
		}
		if !hasSurfaceSlice {
			return log.Errf(ctx, err, "Render stage verification failed: No Surface slice found")
		}
		if !hasRenderSlice {
			return log.Errf(ctx, err, "Render stage verification failed: No Render slice found")
		}
		depths := columns[1].GetLongValues()
		parentStackId := columns[2].GetLongValues()

		for i := skipNum; i < len(names); i++ {
			// Surface slice must be the top level slice, hence its depth is 0 and
			// it has no parent stack id.
			// Render slice must be a non-top-level slice, hence its depth must not be 0
			// and it must have a parent stack id.
			if strings.Contains(names[i], "Surface") {
				if depths[i] != 0 || parentStackId[i] != 0 {
					return log.Errf(ctx, err, "Render stage verification failed on Surface slice")
				}
			} else if strings.Contains(names[i], "Render") {
				if depths[i] <= 0 || parentStackId[i] == 0 {
					return log.Errf(ctx, err, "Render stage verification failed on Render slice")
				}
			}
		}
	}
	return nil
}

func (v *AdrenoValidator) Validate(ctx context.Context, processor *perfetto.Processor) error {
	if err := validate.ValidateGpuCounters(ctx, processor, v.GetCounters()); err != nil {
		return err
	}
	if err := v.validateRenderStage(ctx, processor); err != nil {
		return err
	}

	return nil
}

func (v *AdrenoValidator) GetCounters() []validate.GpuCounter {
	return counters
}
<|endoftext|>