Skip to content

Commit

Permalink
remove: unused functions
Browse files Browse the repository at this point in the history
  • Loading branch information
VanillaSpoon authored and openshift-merge-bot[bot] committed Nov 23, 2023
1 parent 3ca4405 commit a7bb744
Show file tree
Hide file tree
Showing 4 changed files with 0 additions and 338 deletions.
110 changes: 0 additions & 110 deletions controllers/machineset.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,6 @@ const (
timeThirtySeconds = 30 * time.Second
)

func (r *AppWrapperReconciler) checkExistingMachineSet(ctx context.Context, machineSetName string) bool {
// Set up the Object key with the requested app wrapper name and requested type
key := client.ObjectKey{
Name: machineSetName,
Namespace: namespaceToList,
}

machineSet := &machinev1beta1.MachineSet{}
err := r.Get(ctx, key, machineSet)
if err != nil {
// Check if the error is due to the MachineSet not existing
if client.IgnoreNotFound(err) != nil {
// return error if it there is a different error for not getting the MachineSet
klog.Infof("Error getting MachineSet: %s", err)
}
// MachineSet does not exist
return false
}
// The MachineSet exists
return true
}

func (r *AppWrapperReconciler) reconcileCreateMachineSet(ctx context.Context, aw *arbv1.AppWrapper, demandMapPerInstanceType map[string]int) (ctrl.Result, error) {

allMachineSet := machinev1beta1.MachineSetList{}
Expand Down Expand Up @@ -498,94 +476,6 @@ func (r *AppWrapperReconciler) annotateToDeleteMachine(ctx context.Context, aw *
return nil
}

/*
func (r *AppWrapperReconciler) annotateToDeleteMachine(ctx context.Context, aw *arbv1.AppWrapper) error {
label := fmt.Sprintf("%s-%s", aw.Name, aw.Namespace)
// We get a list of Nodes with the AppWrapper name and correct instance type
labelSelector := labels.SelectorFromSet(labels.Set(map[string]string{
label: label,
}))
listOptions := &metav1.ListOptions{
LabelSelector: labelSelector.String(),
}
// List nodes with the AppWrapper name
nodes, _ := r.kubeClient.CoreV1().Nodes().List(ctx, *listOptions)
for _, node := range nodes.Items {
klog.Infof("Filtered node name is %v", aw.Name)
for k, v := range node.Annotations {
if k == "machine.openshift.io/machine" {
machineName := strings.Split(v, "/")
klog.Infof("The machine name to be annotated %v", machineName[1])
allMachines := machinev1beta1.MachineList{}
errm := r.List(ctx, &allMachines)
if errm != nil {
klog.Infof("Error listing machines: %v", errm)
return errm
}
for _, aMachine := range allMachines.Items {
//remove index hardcoding
if aMachine.Name == machineName[1] {
updateMachine := aMachine.DeepCopy()
updateMachine.Annotations["machine.openshift.io/cluster-api-delete-machine"] = "true"
if err := r.Update(ctx, updateMachine); err != nil {
return err
}
var updateMachineset string = ""
for k, v := range updateMachine.Labels {
if k == "machine.openshift.io/cluster-api-machineset" {
updateMachineset = v
klog.Infof("Machineset to update is %v", updateMachineset)
}
}
if updateMachineset != "" {
allMachineSet := machinev1beta1.MachineSetList{}
err := r.List(ctx, &allMachineSet)
if err != nil {
klog.Infof("Machineset retrieval error")
return err
}
for _, aMachineSet := range allMachineSet.Items {
if aMachineSet.Name == updateMachineset {
klog.Infof("Existing machineset replicas %v", &aMachineSet.Spec.Replicas)
//scale down is harded coded to 1??
newReplicas := *aMachineSet.Spec.Replicas - int32(1)
updateMsReplicas := aMachineSet.DeepCopy()
updateMsReplicas.Spec.Replicas = &newReplicas
err := r.Update(ctx, updateMsReplicas)
if err != nil {
klog.Infof("Error updating MachineSet: %s", err)
return err
}
err = r.Get(ctx, types.NamespacedName{Namespace: namespaceToList}, updateMsReplicas)
if err != nil {
klog.Infof("Error updating MachineSet: %s", err)
return err
}
if updateMsReplicas.Labels[fmt.Sprintf("instascale.codeflare.dev-%s-%s", aw.Name, aw.Namespace)] != "" {
if err := r.removeMachineSetLabel(ctx, aw, aMachineSet.Name); err != nil {
return err
}
}
klog.Infof("Replica update successful")
}
}
}
}
}
}
}
}
for machineSetName := range machineSetNames {
if updateMsReplicas.Labels[fmt.Sprintf("instascale.codeflare.dev-%s-%s", aw.Name, aw.Namespace)] != "" {
if err := r.removeMachineSetLabel(ctx, aw, aMachineSet.Name); err != nil {
return err
}
}
}
return nil
}
*/
func (r *AppWrapperReconciler) patchMachineLabels(ctx context.Context, oldAw *arbv1.AppWrapper, newAw *arbv1.AppWrapper, machineName string) error {
// Retrieve the machine object
machine := &machinev1beta1.Machine{}
Expand Down
93 changes: 0 additions & 93 deletions controllers/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,55 +2,15 @@ package controllers

import (
"encoding/json"
"errors"
"fmt"
"math"
"math/rand"
"time"

machinev1 "github.com/openshift/api/machine/v1beta1"
mapiclientset "github.com/openshift/client-go/machine/clientset/versioned"
arbv1 "github.com/project-codeflare/multi-cluster-app-dispatcher/pkg/apis/controller/v1beta1"
clusterstateapi "github.com/project-codeflare/multi-cluster-app-dispatcher/pkg/controller/clusterstate/api"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/klog"
)

// ClientBuilder can create a variety of kubernetes client interface
// with its embeded rest.Config.
type ClientBuilder struct {
config *rest.Config
}

func getPodResourcesWithReplicas(pod arbv1.CustomPodResourceTemplate) (resource *clusterstateapi.Resource, count int) {
replicas := pod.Replicas
req := clusterstateapi.NewResource(pod.Requests)
limit := clusterstateapi.NewResource(pod.Limits)
tolerance := 0.001

// Use limit if request is 0
if diff := math.Abs(req.MilliCPU - float64(0.0)); diff < tolerance {
req.MilliCPU = limit.MilliCPU
}

if diff := math.Abs(req.Memory - float64(0.0)); diff < tolerance {
req.Memory = limit.Memory
}

if req.GPU <= 0 {
req.GPU = limit.GPU
}

return req, replicas
}

// MachineClientOrDie returns the machine api client interface for machine api objects.
func (cb *ClientBuilder) MachineClientOrDie(name string) mapiclientset.Interface {
return mapiclientset.NewForConfigOrDie(rest.AddUserAgent(cb.config, name))
}

func resyncPeriod() func() time.Duration {
return func() time.Duration {
factor := rand.Float64() + 1
Expand All @@ -73,59 +33,6 @@ func ProviderSpecFromRawExtension(rawExtension *runtime.RawExtension) (*machinev
return spec, nil
}

// NewClientBuilder returns a *ClientBuilder with the given kubeconfig.
func NewClientBuilder(kubeconfig string) (*ClientBuilder, error) {
config, err := getRestConfig(kubeconfig)
if err != nil {
return nil, err
}

return &ClientBuilder{
config: config,
}, nil
}

// This function is changed from genericresource.go file as we force to look for resources under custompodresources.
func GetListOfPodResourcesFromOneGenericItem(awr *arbv1.AppWrapperGenericResource) (resource []*clusterstateapi.Resource, er error) {
var podResourcesList []*clusterstateapi.Resource

podTotalresource := clusterstateapi.EmptyResource()
var replicas int
var res *clusterstateapi.Resource
if awr.GenericTemplate.Raw != nil {
podresources := awr.CustomPodResources
for _, item := range podresources {
res, replicas = getPodResourcesWithReplicas(item)
podTotalresource = podTotalresource.Add(res)
}
klog.V(8).Infof("[GetListOfPodResourcesFromOneGenericItem] Requested total allocation resource from 1 pod `%v`.\n", podTotalresource)

// Addd individual pods to results
var replicaCount int = int(replicas)
for i := 0; i < replicaCount; i++ {
podResourcesList = append(podResourcesList, podTotalresource)
}
}

return podResourcesList, nil
}

func getRestConfig(kubeconfig string) (*rest.Config, error) {
var config *rest.Config
var err error
if kubeconfig != "" {
klog.V(10).Infof("Loading kube client config from path %q", kubeconfig)
config, err = clientcmd.BuildConfigFromFlags("", kubeconfig)
} else {
klog.V(4).Infof("Using in-cluster kube client config")
config, err = rest.InClusterConfig()
if err == rest.ErrNotInCluster {
return nil, errors.New("Not running in-cluster? Try using --kubeconfig")
}
}
return config, err
}

func contains(s []string, str string) bool {
for _, v := range s {
if v == str {
Expand Down
121 changes: 0 additions & 121 deletions controllers/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,130 +7,9 @@ import (
"github.com/onsi/gomega"
machinev1 "github.com/openshift/api/machine/v1beta1"

arbv1 "github.com/project-codeflare/multi-cluster-app-dispatcher/pkg/apis/controller/v1beta1"
clusterstateapi "github.com/project-codeflare/multi-cluster-app-dispatcher/pkg/controller/clusterstate/api"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/apimachinery/pkg/runtime"
)

func TestGetPodResourcesWithReplicas(t *testing.T) {
g := gomega.NewGomegaWithT(t)

tests := []struct {
name string
pod arbv1.CustomPodResourceTemplate
wantRes *clusterstateapi.Resource
wantRep int
}{
{
name: "Empty requests and limits",
pod: arbv1.CustomPodResourceTemplate{
Replicas: 0,
Requests: v1.ResourceList{},
Limits: v1.ResourceList{},
},
wantRes: &clusterstateapi.Resource{
MilliCPU: 0,
Memory: 0,
GPU: 0,
},
wantRep: 0,
},
{
name: "Requests and limits",
pod: arbv1.CustomPodResourceTemplate{
Replicas: 3,
Requests: v1.ResourceList{
v1.ResourceCPU: resource.MustParse("1"),
v1.ResourceMemory: resource.MustParse("2Gi"),
v1.ResourceName(clusterstateapi.GPUResourceName): resource.MustParse("1"),
},
Limits: v1.ResourceList{
v1.ResourceCPU: resource.MustParse("2"),
v1.ResourceMemory: resource.MustParse("2Gi"),
v1.ResourceName(clusterstateapi.GPUResourceName): resource.MustParse("2"),
},
},
wantRes: &clusterstateapi.Resource{
MilliCPU: 1000,
Memory: 2 * 1024 * 1024 * 1024,
GPU: 1,
},
wantRep: 3,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
res, rep := getPodResourcesWithReplicas(tt.pod)
g.Expect(res).To(gomega.Equal(tt.wantRes))
g.Expect(rep).To(gomega.Equal(tt.wantRep))
})
}
}

func TestGetListOfPodResourcesFromOneGenericItem(t *testing.T) {
g := gomega.NewGomegaWithT(t)

tests := []struct {
name string
awr *arbv1.AppWrapperGenericResource
want []*clusterstateapi.Resource
}{
{
name: "Empty requests and limits",
awr: &arbv1.AppWrapperGenericResource{
CustomPodResources: []arbv1.CustomPodResourceTemplate{
{
Replicas: 0,
Requests: v1.ResourceList{},
Limits: v1.ResourceList{},
},
},
},
want: []*clusterstateapi.Resource{},
},
{
name: "Request and Limits",
awr: &arbv1.AppWrapperGenericResource{
GenericTemplate: runtime.RawExtension{
Raw: []byte(`{"customPodResources": [{"replicas": 1, "requests": {"gpu": "0", "memory": "1Gi"}, "limits": {"gpu": "0.5", "memory": "2Gi"}}]}`),
},
CustomPodResources: []arbv1.CustomPodResourceTemplate{
{
Replicas: 1,
Requests: v1.ResourceList{
v1.ResourceMemory: resource.MustParse("2Gi"),
v1.ResourceName(clusterstateapi.GPUResourceName): resource.MustParse("1"),
},
Limits: v1.ResourceList{
v1.ResourceMemory: resource.MustParse("2Gi"),
v1.ResourceName(clusterstateapi.GPUResourceName): resource.MustParse("2"),
},
},
},
},
want: []*clusterstateapi.Resource{
{
GPU: 1,
Memory: 2 * 1024 * 1024 * 1024,
},
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, _ := GetListOfPodResourcesFromOneGenericItem(tt.awr)
g.Expect(len(result)).To(gomega.Equal(len(tt.want)))
for i := range result {
g.Expect(result[i]).To(gomega.Equal(tt.want[i]))
}
})
}
}

func TestProviderSpecFromRawExtension(t *testing.T) {
g := gomega.NewGomegaWithT(t)

Expand Down
14 changes: 0 additions & 14 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -408,20 +408,6 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/project-codeflare/multi-cluster-app-dispatcher v1.34.1-0.20230831150053-05b1af83014c h1:LjCgRcjtkd9byePczvxGBLjdM22M2BCTB76FoEk2fkU=
github.com/project-codeflare/multi-cluster-app-dispatcher v1.34.1-0.20230831150053-05b1af83014c/go.mod h1:gtTl8Tsl+X+bGhqVudLoveINR6IkN+sVvH0J+VZIP40=
github.com/project-codeflare/multi-cluster-app-dispatcher v1.34.1 h1:ZNQ/JPdjS6CtaAzt6SNqaoWcpwS1PyVdgZlmIYikPLI=
github.com/project-codeflare/multi-cluster-app-dispatcher v1.34.1/go.mod h1:Yge6GRNpO9YIDfeL+XOcCE9xbmfCTD5C1h5dlW87mxQ=
github.com/project-codeflare/multi-cluster-app-dispatcher v1.35.0 h1:7Ks2+6Jd8tnwymSmSrwNb/CpAPmgS0lNIhMlj6DrT+o=
github.com/project-codeflare/multi-cluster-app-dispatcher v1.35.0/go.mod h1:Yge6GRNpO9YIDfeL+XOcCE9xbmfCTD5C1h5dlW87mxQ=
github.com/project-codeflare/multi-cluster-app-dispatcher v1.36.0 h1:QSc16Kd4OBidGY4rQivMrUX9B9cNKttfZIRKsR4BX9E=
github.com/project-codeflare/multi-cluster-app-dispatcher v1.36.0/go.mod h1:Yge6GRNpO9YIDfeL+XOcCE9xbmfCTD5C1h5dlW87mxQ=
github.com/project-codeflare/multi-cluster-app-dispatcher v1.37.0 h1:oyhdLdc4BgA4zcH1zlRrSrYpzuVxV5QLDbyIXrwnQqs=
github.com/project-codeflare/multi-cluster-app-dispatcher v1.37.0/go.mod h1:Yge6GRNpO9YIDfeL+XOcCE9xbmfCTD5C1h5dlW87mxQ=
github.com/project-codeflare/multi-cluster-app-dispatcher v1.37.1 h1:hZhGwKTPeHYYhNbvO27NOjozVpy7m3I3apKf81u9U3A=
github.com/project-codeflare/multi-cluster-app-dispatcher v1.37.1/go.mod h1:Yge6GRNpO9YIDfeL+XOcCE9xbmfCTD5C1h5dlW87mxQ=
github.com/project-codeflare/multi-cluster-app-dispatcher v1.38.0 h1:dU2Ev0SijdNm30Y9mjdKJL1Fp6l07rnRBKhSbx1kX9g=
github.com/project-codeflare/multi-cluster-app-dispatcher v1.38.0/go.mod h1:Yge6GRNpO9YIDfeL+XOcCE9xbmfCTD5C1h5dlW87mxQ=
github.com/project-codeflare/multi-cluster-app-dispatcher v1.38.1 h1:6ILHYAFxDkAnQu3CJebGQPQGcmcy7/E/AhRiea6yOTc=
github.com/project-codeflare/multi-cluster-app-dispatcher v1.38.1/go.mod h1:Yge6GRNpO9YIDfeL+XOcCE9xbmfCTD5C1h5dlW87mxQ=
github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw=
Expand Down

0 comments on commit a7bb744

Please sign in to comment.