Skip to content

Commit

Permalink
Add debug statements
Browse files Browse the repository at this point in the history
Add debug mode.

Add debug statements for builds and buildruns.
  • Loading branch information
HeavyWombat committed Nov 9, 2020
1 parent f917d05 commit 340fb63
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 2 deletions.
3 changes: 3 additions & 0 deletions internal/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

"github.com/gonvenience/bunt"
"github.com/gonvenience/neat"
"github.com/homeport/build-load/internal/load"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
Expand Down Expand Up @@ -52,6 +53,8 @@ func Execute() {

func init() {
cobra.OnInitialize(initConfig)

rootCmd.PersistentFlags().BoolVar(&load.Debug, "debug", false, "enable additional output messages")
}

func initConfig() {
Expand Down
11 changes: 10 additions & 1 deletion internal/load/buildrun.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func CheckSystemAndConfig(kubeAccess KubeAccess, buildCfg BuildConfig, parallel
}

case http.StatusForbidden:
warn("The current permissions do not allow to check whether build strategy CadetBlue{*%s*} is available.\n\n", buildCfg.ClusterBuildStrategy)
warn("The current permissions do not allow to check whether build strategy CadetBlue{*%s*} is available.\n", buildCfg.ClusterBuildStrategy)
}
}
}
Expand Down Expand Up @@ -147,6 +147,7 @@ func ExecuteSingleBuildRun(kubeAccess KubeAccess, namespace string, name string,
}

defer func() {
debug("Delete build %s", build.Name)
if err := kubeAccess.BuildClient.BuildV1alpha1().Builds(build.Namespace).Delete(build.Name, &metav1.DeleteOptions{}); err != nil {
warn("failed to delete build %s, %v\n", name, err)
}
Expand All @@ -158,6 +159,7 @@ func ExecuteSingleBuildRun(kubeAccess KubeAccess, namespace string, name string,
}

defer func() {
debug("Delete buildrun %s", buildRun.Name)
if err := kubeAccess.BuildClient.BuildV1alpha1().BuildRuns(buildRun.Namespace).Delete(buildRun.Name, &metav1.DeleteOptions{}); err != nil {
warn("failed to delete buildrun %s, %v\n", name, err)
}
Expand All @@ -169,6 +171,7 @@ func ExecuteSingleBuildRun(kubeAccess KubeAccess, namespace string, name string,
}

defer func() {
debug("Delete container image %s", buildRun.Status.BuildSpec.Output.ImageURL)
if err := deleteContainerImage(kubeAccess, buildRun.Namespace, build.Spec.Output.SecretRef, buildRun.Status.BuildSpec.Output.ImageURL); err != nil {
warn("failed to delete image %s, %v\n", buildRun.Status.BuildSpec.Output.ImageURL, err)
}
Expand Down Expand Up @@ -205,6 +208,12 @@ func ExecuteSingleBuildRun(kubeAccess KubeAccess, namespace string, name string,
}
}

debug("buildrun _%s/%s_ results: %v",
namespace,
name,
buildRunResult,
)

return buildRunResult, nil
}

Expand Down
9 changes: 9 additions & 0 deletions internal/load/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ import (
"k8s.io/client-go/tools/clientcmd"
)

// Debug enables or disables additional output
var Debug bool

func lookUpKubeConfigFilePath() (string, error) {
if value, present := os.LookupEnv("KUBECONFIG"); present {
return value, nil
Expand Down Expand Up @@ -106,3 +109,9 @@ func wrapErrorChanResults(errors chan error, format string, a ...interface{}) er
func warn(format string, a ...interface{}) {
bunt.Printf("DarkOrange{*Warning:*} %s\n", bunt.Sprintf(format, a...))
}

func debug(format string, a ...interface{}) {
if Debug {
bunt.Printf("DimGray{*Debug:*} %s\n", bunt.Sprintf(format, a...))
}
}
6 changes: 5 additions & 1 deletion internal/load/kubeops.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,15 @@ func newBuildRun(name string, build buildv1alpha1.Build, generateServiceAccount
}

func applyBuild(kubeAccess KubeAccess, build buildv1alpha1.Build) (*buildv1alpha1.Build, error) {
debug("Create build %s", build.Name)
return kubeAccess.BuildClient.
BuildV1alpha1().
Builds(build.Namespace).
Create(&build)
}

func applyBuildRun(kubeAccess KubeAccess, buildRun buildv1alpha1.BuildRun) (*buildv1alpha1.BuildRun, error) {
debug("Create buildrun %s", buildRun.Name)
return kubeAccess.BuildClient.
BuildV1alpha1().
BuildRuns(buildRun.Namespace).
Expand All @@ -94,6 +96,7 @@ func applyBuildRun(kubeAccess KubeAccess, buildRun buildv1alpha1.BuildRun) (*bui
func waitForBuildRunCompletion(kubeAccess KubeAccess, buildRun *buildv1alpha1.BuildRun) (*buildv1alpha1.BuildRun, error) {
var (
timeout = defaultBuildRunWaitTimeout
interval = 5 * time.Second
namespace = buildRun.Namespace
name = buildRun.Name
)
Expand All @@ -102,7 +105,8 @@ func waitForBuildRunCompletion(kubeAccess KubeAccess, buildRun *buildv1alpha1.Bu
timeout = buildRun.Spec.Timeout.Duration
}

err := wait.PollImmediate(5*time.Second, timeout, func() (done bool, err error) {
debug("Polling every %v to wait for completion of buildrun %s", interval, buildRun.Name)
err := wait.PollImmediate(interval, timeout, func() (done bool, err error) {
buildRun, err = kubeAccess.BuildClient.BuildV1alpha1().BuildRuns(namespace).Get(name, metav1.GetOptions{})
if err != nil {
return false, err
Expand Down
19 changes: 19 additions & 0 deletions internal/load/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/gonvenience/bunt"
"gopkg.in/yaml.v3"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
Expand Down Expand Up @@ -94,6 +95,24 @@ type TestPlan struct {
} `yaml:"steps" json:"steps"`
}

func (brr BuildRunResult) String() string {
var duration = func(d time.Duration) string {
if d > time.Duration(0) {
return d.String()
}

return bunt.Sprintf("DarkGray{_(no data)_}")
}

return strings.Join([]string{
bunt.Sprintf("_TotalBuildRunTime_=%v", duration(brr.TotalBuildRunTime)),
bunt.Sprintf("_BuildRunRampUpDuration_=%v", duration(brr.BuildRunRampUpDuration)),
bunt.Sprintf("_TaskRunRampUpDuration_=%v", duration(brr.TaskRunRampUpDuration)),
bunt.Sprintf("_PodRampUpDuration_=%v", duration(brr.PodRampUpDuration)),
bunt.Sprintf("_InternalProcessingTime_=%v", duration(brr.InternalProcessingTime)),
}, ", ")
}

// NewTestPlan creates a test plan based on the provided input
func NewTestPlan(in io.Reader) (*TestPlan, error) {
data, err := ioutil.ReadAll(in)
Expand Down

0 comments on commit 340fb63

Please sign in to comment.