Skip to content

Commit

Permalink
Introduce common context
Browse files Browse the repository at this point in the history
Add context to kube access helper.

Use common context in all calls requiring a context.
  • Loading branch information
HeavyWombat committed May 20, 2021
1 parent de0569d commit 2053c7e
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 20 deletions.
3 changes: 1 addition & 2 deletions internal/load/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package load

import (
"context"
"fmt"
"sync"
"time"
Expand Down Expand Up @@ -98,7 +97,7 @@ func waitForBuildRegistered(kubeAccess KubeAccess, build *buildv1alpha1.Build) (

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{})
build, err = kubeAccess.BuildClient.BuildV1alpha1().Builds(namespace).Get(kubeAccess.Context, name, metav1.GetOptions{})
if err != nil {
return false, err
}
Expand Down
9 changes: 4 additions & 5 deletions internal/load/buildrun.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package load

import (
"context"
"fmt"
"net/http"
"strings"
Expand Down Expand Up @@ -46,15 +45,15 @@ type BuildRunOption func(*buildRunOptions)
// would put onto the system
func CheckSystemAndConfig(kubeAccess KubeAccess, buildCfg BuildConfig, parallel int) error {
// Check whether the configured cluster build strategy is available
clusterBuildStrategy, err := kubeAccess.BuildClient.BuildV1alpha1().ClusterBuildStrategies().Get(context.TODO(), buildCfg.ClusterBuildStrategy, metav1.GetOptions{})
clusterBuildStrategy, err := kubeAccess.BuildClient.BuildV1alpha1().ClusterBuildStrategies().Get(kubeAccess.Context, buildCfg.ClusterBuildStrategy, metav1.GetOptions{})
if err != nil {
clusterBuildStrategy = nil

switch terr := err.(type) {
case *errors.StatusError:
switch terr.ErrStatus.Code {
case http.StatusNotFound:
if list, _ := kubeAccess.BuildClient.BuildV1alpha1().ClusterBuildStrategies().List(context.TODO(), metav1.ListOptions{}); list != nil {
if list, _ := kubeAccess.BuildClient.BuildV1alpha1().ClusterBuildStrategies().List(kubeAccess.Context, metav1.ListOptions{}); list != nil {
var names = make([]string, len(list.Items))
for i, entry := range list.Items {
names[i] = entry.GetName()
Expand All @@ -78,7 +77,7 @@ func CheckSystemAndConfig(kubeAccess KubeAccess, buildCfg BuildConfig, parallel

// Given that the permissions allow it, check how many buildruns are
// currently in the system already
if buildRunsResults, err := kubeAccess.BuildClient.BuildV1alpha1().BuildRuns("").List(context.TODO(), metav1.ListOptions{}); err == nil {
if buildRunsResults, err := kubeAccess.BuildClient.BuildV1alpha1().BuildRuns("").List(kubeAccess.Context, metav1.ListOptions{}); err == nil {
var (
totalBuildRuns int
completedBuildRuns int
Expand Down Expand Up @@ -109,7 +108,7 @@ func CheckSystemAndConfig(kubeAccess KubeAccess, buildCfg BuildConfig, parallel
}
}

if nodesResults, err := kubeAccess.Client.CoreV1().Nodes().List(context.TODO(), metav1.ListOptions{}); err == nil {
if nodesResults, err := kubeAccess.Client.CoreV1().Nodes().List(kubeAccess.Context, metav1.ListOptions{}); err == nil {
var totalCPU int64
var totalMemory int64
for _, node := range nodesResults.Items {
Expand Down
2 changes: 2 additions & 0 deletions internal/load/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package load

import (
"context"
"os"
"path/filepath"

Expand Down Expand Up @@ -82,6 +83,7 @@ func NewKubeAccess() (*KubeAccess, error) {
}

return &KubeAccess{
Context: context.Background(),
RestConfig: restConfig,
Client: client,
BuildClient: buildClient,
Expand Down
26 changes: 13 additions & 13 deletions internal/load/kubeops.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func applyBuild(kubeAccess KubeAccess, build buildv1alpha1.Build) (*buildv1alpha
return kubeAccess.BuildClient.
BuildV1alpha1().
Builds(build.Namespace).
Create(context.TODO(), &build, metav1.CreateOptions{})
Create(kubeAccess.Context, &build, metav1.CreateOptions{})
}

func applyBuildRun(kubeAccess KubeAccess, buildRun buildv1alpha1.BuildRun) (*buildv1alpha1.BuildRun, error) {
Expand All @@ -108,41 +108,41 @@ func applyBuildRun(kubeAccess KubeAccess, buildRun buildv1alpha1.BuildRun) (*bui
return kubeAccess.BuildClient.
BuildV1alpha1().
BuildRuns(buildRun.Namespace).
Create(context.TODO(), &buildRun, metav1.CreateOptions{})
Create(kubeAccess.Context, &buildRun, metav1.CreateOptions{})
}

func deleteBuild(kubeAccess KubeAccess, namespace string, name string, deleteOptions *metav1.DeleteOptions) error {
_, err := kubeAccess.BuildClient.BuildV1alpha1().Builds(namespace).Get(context.TODO(), name, metav1.GetOptions{})
_, err := kubeAccess.BuildClient.BuildV1alpha1().Builds(namespace).Get(kubeAccess.Context, name, metav1.GetOptions{})
if errors.IsNotFound(err) {
return nil
}

debug("Delete build %s", name)
if err := kubeAccess.BuildClient.BuildV1alpha1().Builds(namespace).Delete(context.TODO(), name, *deleteOptions); err != nil {
if err := kubeAccess.BuildClient.BuildV1alpha1().Builds(namespace).Delete(kubeAccess.Context, name, *deleteOptions); err != nil {
return err
}

return wait.PollImmediate(1*time.Second, 10*time.Second, func() (done bool, err error) {
_, err = kubeAccess.BuildClient.BuildV1alpha1().Builds(namespace).Get(context.TODO(), name, metav1.GetOptions{})
_, err = kubeAccess.BuildClient.BuildV1alpha1().Builds(namespace).Get(kubeAccess.Context, name, metav1.GetOptions{})
return errors.IsNotFound(err), nil
})
}

func deleteBuildRun(kubeAccess KubeAccess, namespace string, name string, deleteOptions *metav1.DeleteOptions) error {
buildRun, err := kubeAccess.BuildClient.BuildV1alpha1().BuildRuns(namespace).Get(context.TODO(), name, metav1.GetOptions{})
buildRun, err := kubeAccess.BuildClient.BuildV1alpha1().BuildRuns(namespace).Get(kubeAccess.Context, name, metav1.GetOptions{})
if errors.IsNotFound(err) {
return nil
}

_, pod := lookUpTaskRunAndPod(kubeAccess, *buildRun)

debug("Delete buildrun %s", name)
if err := kubeAccess.BuildClient.BuildV1alpha1().BuildRuns(namespace).Delete(context.TODO(), name, *deleteOptions); err != nil {
if err := kubeAccess.BuildClient.BuildV1alpha1().BuildRuns(namespace).Delete(kubeAccess.Context, name, *deleteOptions); err != nil {
return err
}

err = wait.PollImmediate(1*time.Second, 10*time.Second, func() (done bool, err error) {
_, err = kubeAccess.BuildClient.BuildV1alpha1().BuildRuns(namespace).Get(context.TODO(), name, metav1.GetOptions{})
_, err = kubeAccess.BuildClient.BuildV1alpha1().BuildRuns(namespace).Get(kubeAccess.Context, name, metav1.GetOptions{})
return errors.IsNotFound(err), nil
})

Expand All @@ -152,7 +152,7 @@ func deleteBuildRun(kubeAccess KubeAccess, namespace string, name string, delete

if pod != nil {
err = wait.PollImmediate(1*time.Second, 10*time.Second, func() (done bool, err error) {
_, err = kubeAccess.Client.CoreV1().Pods(namespace).Get(context.TODO(), pod.Name, metav1.GetOptions{})
_, err = kubeAccess.Client.CoreV1().Pods(namespace).Get(kubeAccess.Context, pod.Name, metav1.GetOptions{})
return errors.IsNotFound(err), nil
})
}
Expand Down Expand Up @@ -213,7 +213,7 @@ func lookUpTaskRunAndPod(kubeAccess KubeAccess, buildRun buildv1alpha1.BuildRun)
tmp, err := kubeAccess.Client.
CoreV1().
Pods(taskRun.Namespace).
Get(context.TODO(), taskRun.Status.PodName, metav1.GetOptions{})
Get(kubeAccess.Context, taskRun.Status.PodName, metav1.GetOptions{})

if err == nil {
taskRunPod = tmp
Expand All @@ -227,7 +227,7 @@ func lookUpTaskRunAndPod(kubeAccess KubeAccess, buildRun buildv1alpha1.BuildRun)
listResp, err := kubeAccess.Client.
CoreV1().
Pods(buildRun.Namespace).
List(context.TODO(), metav1.ListOptions{
List(kubeAccess.Context, metav1.ListOptions{
LabelSelector: fmt.Sprintf("buildrun.build.dev/name=%s", buildRun.Name)},
)

Expand All @@ -239,7 +239,7 @@ func lookUpTaskRunAndPod(kubeAccess KubeAccess, buildRun buildv1alpha1.BuildRun)
}

func lookUpDockerCredentialsFromSecret(kubeAccess KubeAccess, namespace string, secretRef *corev1.LocalObjectReference) (string, string, error) {
secret, err := kubeAccess.Client.CoreV1().Secrets(namespace).Get(context.TODO(), secretRef.Name, metav1.GetOptions{})
secret, err := kubeAccess.Client.CoreV1().Secrets(namespace).Get(kubeAccess.Context, secretRef.Name, metav1.GetOptions{})
if err != nil {
return "", "", err
}
Expand Down Expand Up @@ -311,7 +311,7 @@ func buildRunError(kubeAccess KubeAccess, buildRun buildv1alpha1.BuildRun) error
Resource("pods").
SubResource("log").
Param("container", container.Name).
Stream(context.TODO())
Stream(kubeAccess.Context)

if err == nil {
defer reader.Close()
Expand Down
2 changes: 2 additions & 0 deletions internal/load/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package load

import (
"context"
"encoding/json"
"fmt"
"io"
Expand Down Expand Up @@ -50,6 +51,7 @@ const (

// KubeAccess contains Kubernetes cluster access objects in a single place
type KubeAccess struct {
Context context.Context
RestConfig *rest.Config
Client kubernetes.Interface
BuildClient buildclient.Interface
Expand Down

0 comments on commit 2053c7e

Please sign in to comment.