Skip to content

Support package refactor #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*.dll
*.so
*.dylib
bin

# Test binary, built with `go test -c`
*.test
Expand Down
20 changes: 20 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
## Location to install dependencies to
LOCALBIN ?= $(shell pwd)/bin
$(LOCALBIN):
mkdir -p $(LOCALBIN)

OPENSHIFT-GOIMPORTS ?= $(LOCALBIN)/openshift-goimports


.PHONY: openshift-goimports
openshift-goimports: $(OPENSHIFT-GOIMPORTS) ## Download openshift-goimports locally if necessary.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should bring the corresponding GH actions workflows there too.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

verify_generated_files workflow added.

$(OPENSHIFT-GOIMPORTS): $(LOCALBIN)
test -s $(LOCALBIN)/openshift-goimports || GOBIN=$(LOCALBIN) go install github.com/openshift-eng/openshift-goimports@latest

.PHONY: imports
imports: openshift-goimports ## Organize imports in go files using openshift-goimports. Example: make imports
$(OPENSHIFT-GOIMPORTS)

.PHONY: verify-imports
verify-imports: openshift-goimports ## Run import verifications.
./hack/verify-imports.sh $(OPENSHIFT-GOIMPORTS)
69 changes: 69 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
module github.com/project-codeflare/codeflare-common

go 1.19

require (
github.com/onsi/gomega v1.28.0
github.com/openshift-online/ocm-sdk-go v0.1.373
github.com/openshift/api v0.0.0-20231012181603-8f468d7b55a8
github.com/openshift/client-go v0.0.0-20231005121823-e81400b97c46
github.com/project-codeflare/multi-cluster-app-dispatcher v1.36.0
github.com/ray-project/kuberay/ray-operator v0.0.0-20231013074931-1184bc872c08
k8s.io/api v0.28.2
k8s.io/apimachinery v0.28.2
k8s.io/client-go v0.28.2
)

require (
github.com/aymerick/douceur v0.2.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cenkalti/backoff/v4 v4.1.3 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emicklei/go-restful/v3 v3.10.1 // indirect
github.com/go-logr/logr v1.2.4 // indirect
github.com/go-openapi/jsonpointer v0.19.6 // indirect
github.com/go-openapi/jsonreference v0.20.2 // indirect
github.com/go-openapi/swag v0.22.3 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang-jwt/jwt/v4 v4.4.1 // indirect
github.com/golang/glog v1.0.0 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/gnostic-models v0.6.8 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/gorilla/css v1.0.0 // indirect
github.com/imdario/mergo v0.3.12 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.2 // indirect
github.com/microcosm-cc/bluemonday v1.0.18 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/prometheus/client_golang v1.14.0 // indirect
github.com/prometheus/client_model v0.3.0 // indirect
github.com/prometheus/common v0.37.0 // indirect
github.com/prometheus/procfs v0.8.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/net v0.14.0 // indirect
golang.org/x/oauth2 v0.8.0 // indirect
golang.org/x/sys v0.11.0 // indirect
golang.org/x/term v0.11.0 // indirect
golang.org/x/text v0.12.0 // indirect
golang.org/x/time v0.3.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.30.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/klog/v2 v2.100.1 // indirect
k8s.io/kube-openapi v0.0.0-20230717233707-2695361300d9 // indirect
k8s.io/utils v0.0.0-20230505201702-9f6742963106 // indirect
sigs.k8s.io/controller-runtime v0.11.1 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
)
771 changes: 771 additions & 0 deletions go.sum

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions hack/verify-imports.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash
OPENSHIFT_GOIMPORTS=${1}

# ${OPENSHIFT_GOIMPORTS} -l
bad_files=$(${OPENSHIFT_GOIMPORTS} -l)

echo $bad_files
if [[ -n ${bad_files} ]]; then
echo "!!! openshift-goimports needs to be run on the following files:"
echo "${bad_files}"
echo "Try running 'make imports'"
exit 1
fi
37 changes: 37 additions & 0 deletions support/batch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
Copyright 2023.

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 support

import (
"github.com/onsi/gomega"

batchv1 "k8s.io/api/batch/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func Job(t Test, namespace, name string) func(g gomega.Gomega) *batchv1.Job {
return func(g gomega.Gomega) *batchv1.Job {
job, err := t.Client().Core().BatchV1().Jobs(namespace).Get(t.Ctx(), name, metav1.GetOptions{})
g.Expect(err).NotTo(gomega.HaveOccurred())
return job
}
}

func GetJob(t Test, namespace, name string) *batchv1.Job {
t.T().Helper()
return Job(t, namespace, name)(t)
}
124 changes: 124 additions & 0 deletions support/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/*
Copyright 2023.

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 support

import (
mcadclient "github.com/project-codeflare/multi-cluster-app-dispatcher/pkg/client/clientset/versioned"
rayclient "github.com/ray-project/kuberay/ray-operator/pkg/client/clientset/versioned"

"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
_ "k8s.io/client-go/plugin/pkg/client/auth"
"k8s.io/client-go/tools/clientcmd"

imagev1 "github.com/openshift/client-go/image/clientset/versioned"
routev1 "github.com/openshift/client-go/route/clientset/versioned"
// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
// to ensure that exec-entrypoint and run can make use of them.
)

type Client interface {
Core() kubernetes.Interface
Route() routev1.Interface
Image() imagev1.Interface
MCAD() mcadclient.Interface
Ray() rayclient.Interface
Dynamic() dynamic.Interface
}

type testClient struct {
core kubernetes.Interface
route routev1.Interface
image imagev1.Interface
mcad mcadclient.Interface
ray rayclient.Interface
dynamic dynamic.Interface
}

var _ Client = (*testClient)(nil)

func (t *testClient) Core() kubernetes.Interface {
return t.core
}

func (t *testClient) Route() routev1.Interface {
return t.route
}

func (t *testClient) Image() imagev1.Interface {
return t.image
}
func (t *testClient) MCAD() mcadclient.Interface {
return t.mcad
}

func (t *testClient) Ray() rayclient.Interface {
return t.ray
}

func (t *testClient) Dynamic() dynamic.Interface {
return t.dynamic
}

func newTestClient() (Client, error) {
cfg, err := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
clientcmd.NewDefaultClientConfigLoadingRules(),
&clientcmd.ConfigOverrides{},
).ClientConfig()
if err != nil {
return nil, err
}

kubeClient, err := kubernetes.NewForConfig(cfg)
if err != nil {
return nil, err
}

routeClient, err := routev1.NewForConfig(cfg)
if err != nil {
return nil, err
}

imageClient, err := imagev1.NewForConfig(cfg)
if err != nil {
return nil, err
}

mcadClient, err := mcadclient.NewForConfig(cfg)
if err != nil {
return nil, err
}

rayClient, err := rayclient.NewForConfig(cfg)
if err != nil {
return nil, err
}

dynamicClient, err := dynamic.NewForConfig(cfg)
if err != nil {
return nil, err
}

return &testClient{
core: kubeClient,
route: routeClient,
image: imageClient,
mcad: mcadClient,
ray: rayClient,
dynamic: dynamicClient,
}, nil
}
85 changes: 85 additions & 0 deletions support/conditions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You 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 support

import (
appsv1 "k8s.io/api/apps/v1"
batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"

routev1 "github.com/openshift/api/route/v1"
)

type conditionType interface {
~string
}

func ConditionStatus[T conditionType](conditionType T) func(any) corev1.ConditionStatus {
return func(object any) corev1.ConditionStatus {
switch o := object.(type) {

case *batchv1.Job:
if c := getJobCondition(o.Status.Conditions, batchv1.JobConditionType(conditionType)); c != nil {
return c.Status
}
case *appsv1.Deployment:
if c := getDeploymentCondition(o.Status.Conditions, appsv1.DeploymentConditionType(conditionType)); c != nil {
return c.Status
}
case *routev1.Route:
if len(o.Status.Ingress) == 0 {
// Route is not initialized yet
break
}
if c := getRouteCondition(o.Status.Ingress[0].Conditions, routev1.RouteIngressConditionType(conditionType)); c != nil {
return c.Status
}
}

return corev1.ConditionUnknown
}
}

// TODO: to be replaced with a generic version once common struct fields of a type set can be used.
// See https://github.com/golang/go/issues/48522
func getJobCondition(conditions []batchv1.JobCondition, conditionType batchv1.JobConditionType) *batchv1.JobCondition {
for _, c := range conditions {
if c.Type == conditionType {
return &c
}
}
return nil
}

func getDeploymentCondition(conditions []appsv1.DeploymentCondition, conditionType appsv1.DeploymentConditionType) *appsv1.DeploymentCondition {
for _, c := range conditions {
if c.Type == conditionType {
return &c
}
}
return nil
}

func getRouteCondition(conditions []routev1.RouteIngressCondition, conditionType routev1.RouteIngressConditionType) *routev1.RouteIngressCondition {
for _, c := range conditions {
if c.Type == conditionType {
return &c
}
}
return nil
}
Loading