Skip to content

Commit

Permalink
Introduce build-load builds command to measure build registration time
Browse files Browse the repository at this point in the history
  • Loading branch information
SaschaSchwarze0 authored and HeavyWombat committed Feb 25, 2021
1 parent e89af13 commit c3a5f57
Show file tree
Hide file tree
Showing 17 changed files with 864 additions and 127 deletions.
21 changes: 15 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,31 @@ go 1.15

require (
github.com/dgrijalva/jwt-go v3.2.0+incompatible
github.com/docker/cli v0.0.0-20200210162036-a4bedce16568 // indirect
github.com/gonvenience/bunt v1.1.4
github.com/gonvenience/neat v1.3.5
github.com/gonvenience/text v1.0.6
github.com/gonvenience/wrap v1.1.0
github.com/google/go-github/v29 v29.0.3 // indirect
github.com/lucasb-eyer/go-colorful v1.0.3
github.com/mitchellh/go-homedir v1.1.0
github.com/onsi/ginkgo v1.14.2
github.com/onsi/gomega v1.10.3
github.com/shipwright-io/build v0.1.1
github.com/shipwright-io/build v0.3.0
github.com/spf13/cobra v1.0.0
github.com/spf13/viper v1.7.1
github.com/tektoncd/pipeline v0.14.2
github.com/tektoncd/pipeline v0.20.1
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776
k8s.io/api v0.17.6
k8s.io/apimachinery v0.17.6
k8s.io/api v0.18.12
k8s.io/apimachinery v0.19.0
k8s.io/client-go v12.0.0+incompatible
k8s.io/utils v0.0.0-20200124190032-861946025e34
knative.dev/pkg v0.0.0-20200528142800-1c6815d7e4c9
k8s.io/utils v0.0.0-20200603063816-c1c6865ac451
knative.dev/pkg v0.0.0-20210107022335-51c72e24c179
knative.dev/test-infra v0.0.0-20200519015156-82551620b0a9 // indirect
)

replace (
github.com/googleapis/gnostic => github.com/googleapis/gnostic v0.4.0
k8s.io/client-go => k8s.io/client-go v0.18.10 // Required by prometheus-operator
k8s.io/kube-openapi => k8s.io/kube-openapi v0.0.0-20200410145947-bcb3869e6f29 // resolve `case-insensitive import collision` for gnostic/openapiv2 package
)
446 changes: 446 additions & 0 deletions go.sum

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion internal/cmd/buildrun-series.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ var buildRunSeriesCmd = &cobra.Command{
})

store(buildRunSeriesCmdSettings.csvOutput, func(w io.Writer) error {
return load.CreateBuildRunResultSetCSV(results, w)
return load.CreateResultSetCSV(results, w)
})

return nil
Expand Down
4 changes: 2 additions & 2 deletions internal/cmd/buildrun-single.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ var buildRunOnceCmd = &cobra.Command{
})

store(buildRunOnceCmdSettings.csvOutput, func(w io.Writer) error {
return load.CreateBuildRunResultsCSV(buildRunResults, w)
return load.CreateResultsCSV(buildRunResults, w)
})

fmt.Print(load.CalculateBuildRunResultSet(buildRunResults))
fmt.Print(load.CalculateResultSet(buildRunResults, "buildrun"))

return nil
},
Expand Down
82 changes: 82 additions & 0 deletions internal/cmd/builds.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
Copyright © 2021 The Homeport Team
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 cmd

import (
"fmt"

"github.com/gonvenience/bunt"
"github.com/gonvenience/wrap"
"github.com/homeport/build-load/internal/load"
"github.com/spf13/cobra"
)

var buildsCmdSettings struct {
count int
namingCfg load.NamingConfig
buildCfg load.BuildConfig

htmlOutput string
csvOutput string
}

var buildsCmd = &cobra.Command{
Use: "builds",
Short: "Creates a series of builds",
Long: bunt.Sprintf("*Creates a series of builds*\n\nWaits for them to be registered."),
SilenceUsage: true,
SilenceErrors: true,

PreRunE: func(cmd *cobra.Command, args []string) error {
if buildsCmdSettings.count <= 0 {
return wrap.Errorf(
fmt.Errorf(cmd.UsageString()),
"input parameter for count is out bounds",
)
}

return nil
},

RunE: func(cmd *cobra.Command, args []string) error {
kubeAccess, err := load.NewKubeAccess()
if err != nil {
return err
}

buildResults, err := load.ExecuteBuilds(*kubeAccess, buildsCmdSettings.namingCfg, buildsCmdSettings.buildCfg, buildsCmdSettings.count)
if err != nil {
return err
}

fmt.Print(load.CalculateResultSet(buildResults, "build"))

return nil
},
}

func init() {
rootCmd.AddCommand(buildsCmd)

buildsCmd.Flags().SortFlags = false
buildsCmd.PersistentFlags().SortFlags = false

buildsCmd.Flags().IntVar(&buildsCmdSettings.count, "count", 5, "Number of builds")

applyNamingFlags(buildsCmd, &buildsCmdSettings.namingCfg)
applyBuildRunSettingsFlags(buildsCmd, &buildsCmdSettings.buildCfg)
}
1 change: 1 addition & 0 deletions internal/cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ func applyBuildRunSettingsFlags(cmd *cobra.Command, buildCfg *load.BuildConfig)
pf.StringVar(&buildCfg.SourceRevision, "source-revision", "master", "specify the branch, tag, or commit to be used")
pf.StringVar(&buildCfg.SourceSecretRef, "source-secret", "", "specify secret to be used to access the source")
pf.StringVar(&buildCfg.SourceDockerfile, "dockerfile", "Dockerfile", "specify name of the docker file for kaniko builds")
pf.BoolVar(&buildCfg.SkipVerifySourceRepository, "skip-verify-repository", false, "skip the verification of the source repository")

pf.StringVar(&buildCfg.OutputImageURL, "output-image-url", "", "output image URL")
pf.StringVar(&buildCfg.OutputSecretRef, "output-secret-ref", "", "secret that contains the access credentials for the output registry")
Expand Down
168 changes: 168 additions & 0 deletions internal/load/build.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
/*
Copyright © 2021 The Homeport Team
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 load

import (
"context"
"fmt"
"sync"
"time"

buildv1alpha1 "github.com/shipwright-io/build/pkg/apis/build/v1alpha1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/wait"
)

func buildError(build buildv1alpha1.Build) error {
if build.Status.Registered == corev1.ConditionTrue {
return nil
}

return fmt.Errorf("build failed to register. Reason=%s. Message=%s", build.Status.Reason, build.Status.Message)
}

func registerSingleBuild(kubeAccess KubeAccess, namespace string, name string, buildSpec buildv1alpha1.BuildSpec, buildAnnotations map[string]string, options ...BuildRunOption) (*Result, error) {
var buildRunOptions = buildRunOptions{}
for _, option := range options {
option(&buildRunOptions)
}

build, err := applyBuild(kubeAccess, newBuild(namespace, name, buildSpec, buildAnnotations))
if err != nil {
return nil, err
}

var graceperiod int64 = int64(0)
deleteOptions := metav1.DeleteOptions{GracePeriodSeconds: &graceperiod}

if !buildRunOptions.skipDelete {
defer func() {
if err := deleteBuild(kubeAccess, build.Namespace, build.Name, &deleteOptions); err != nil {
warn("failed to delete build %s, %v\n", name, err)
}
}()
}

build, err = waitForBuildRegistered(kubeAccess, build)
if err != nil {
return nil, err
}

var buildRegisteredTime time.Time

for _, mf := range build.ManagedFields {
if mf.Operation == metav1.ManagedFieldsOperationUpdate && mf.Manager == "build-operator" {
buildRegisteredTime = mf.Time.Time
}
}

if buildRegisteredTime.IsZero() {
return nil, fmt.Errorf("did not find update time for build %s", build.Name)
}

var buildResult = Result{
Value{
BuildRegistrationTime,
duration(build.CreationTimestamp.Time, buildRegisteredTime),
},
}

debug("build _%s/%s_ results: %v",
namespace,
name,
buildResult,
)

return &buildResult, nil
}

func waitForBuildRegistered(kubeAccess KubeAccess, build *buildv1alpha1.Build) (*buildv1alpha1.Build, error) {
var (
timeout = defaultBuildRunWaitTimeout
interval = 5 * time.Second
namespace = build.Namespace
name = build.Name
)

debug("Polling every %v to wait for registration of build %s", interval, build.Name)
err := wait.PollImmediate(interval, timeout, func() (done bool, err error) {
build, err = kubeAccess.BuildClient.BuildV1alpha1().Builds(namespace).Get(context.TODO(), name, metav1.GetOptions{})
if err != nil {
return false, err
}

switch build.Status.Registered {
case corev1.ConditionTrue:
return true, nil

case corev1.ConditionFalse:
return false, buildError(*build)
}

return false, nil
})

return build, err
}

// ExecuteBuilds creates a number of builds and waits for them to be registered
func ExecuteBuilds(kubeAccess KubeAccess, namingCfg NamingConfig, buildCfg BuildConfig, count int) ([]Result, error) {

var errors = make(chan error, count)
var wg sync.WaitGroup
wg.Add(count)

var buildResults = make([]Result, count)
for i := 0; i < count; i++ {
go func(idx int) {
defer wg.Done()

namespace, name := createNamespaceAndName(namingCfg, buildCfg, idx)

buildSpec, err := createBuildSpec(name, buildCfg)
if err != nil {
errors <- err
return
}

buildAnnotations := createBuildAnnotations(buildCfg)

result, err := registerSingleBuild(
kubeAccess,
namespace,
name,
*buildSpec,
buildAnnotations,
GenerateServiceAccount(buildCfg.GenerateServiceAccount),
SkipDelete(buildCfg.SkipDelete),
)

if err != nil {
errors <- err
return
}

buildResults[idx] = *result
}(i)
}

wg.Wait()
close(errors)

return buildResults, wrapErrorChanResults(errors, "failed to execute buildruns")
}
Loading

0 comments on commit c3a5f57

Please sign in to comment.