Skip to content
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

Created ResourceQuotaOps #418

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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 k8s/core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ type Ops interface {
LimitRangeOps
NetworkPolicyOps
CertificateOps
ResourceQuotaOps

// SetConfig sets the config and resets the client
SetConfig(config *rest.Config)
Expand Down
109 changes: 109 additions & 0 deletions k8s/core/resourcequota.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package core

import (
"context"

"github.com/sirupsen/logrus"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
)

// ResourceQuotaOps is an interface to perform k8s ResourceQuota operations
type ResourceQuotaOps interface {
// GetResourceQuota gets the resource quota object for the given name and namespace
GetResourceQuota(name string, namespace string) (*corev1.ResourceQuota, error)
// CreateResourceQuota creates a new resource quota object if it does not already exist.
CreateResourceQuota(resourceQuota *corev1.ResourceQuota) (*corev1.ResourceQuota, error)
// DeleteResourceQuota deletes the given resource quota
DeleteResourceQuota(name, namespace string) error
// UpdateResourceQuota updates the given resource quota object
UpdateResourceQuota(resourceQuota *corev1.ResourceQuota) (*corev1.ResourceQuota, error)
// WatchResourceQuota sets up a watcher that listens for changes on the resource quota
WatchResourceQuota(resourceQuota *corev1.ResourceQuota, fn WatchFunc) error
// ListResourceQuota returns the list of ResourceQuotas
ListResourceQuota(namespace string, filterOptions metav1.ListOptions) (*corev1.ResourceQuotaList, error)
}

// GetResourceQuota gets the resource quota object for the given name and namespace
func (c *Client) GetResourceQuota(name string, namespace string) (*corev1.ResourceQuota, error) {
if err := c.initClient(); err != nil {
return nil, err
}

return c.kubernetes.CoreV1().ResourceQuotas(namespace).Get(context.TODO(), name, metav1.GetOptions{})
}

// CreateResourceQuota creates a new resource quota object if it does not already exist.
func (c *Client) CreateResourceQuota(resourceQuota *corev1.ResourceQuota) (*corev1.ResourceQuota, error) {
if err := c.initClient(); err != nil {
return nil, err
}

ns := resourceQuota.Namespace
if len(ns) == 0 {
ns = corev1.NamespaceDefault
}

return c.kubernetes.CoreV1().ResourceQuotas(ns).Create(context.TODO(), resourceQuota, metav1.CreateOptions{})
}

// DeleteResourceQuota deletes the given resource quota
func (c *Client) DeleteResourceQuota(name, namespace string) error {
if err := c.initClient(); err != nil {
return err
}

if len(namespace) == 0 {
namespace = corev1.NamespaceDefault
}

return c.kubernetes.CoreV1().ResourceQuotas(namespace).Delete(context.TODO(), name, metav1.DeleteOptions{
PropagationPolicy: &deleteForegroundPolicy,
})
}

// UpdateResourceQuota updates the given resource quota object
func (c *Client) UpdateResourceQuota(resourceQuota *corev1.ResourceQuota) (*corev1.ResourceQuota, error) {
if err := c.initClient(); err != nil {
return nil, err
}

ns := resourceQuota.Namespace
if len(ns) == 0 {
ns = corev1.NamespaceDefault
}

return c.kubernetes.CoreV1().ResourceQuotas(ns).Update(context.TODO(), resourceQuota, metav1.UpdateOptions{})
}

// WatchResourceQuota sets up a watcher that listens for changes on the resource quota
func (c *Client) WatchResourceQuota(resourceQuota *corev1.ResourceQuota, fn WatchFunc) error {
if err := c.initClient(); err != nil {
return err
}

listOptions := metav1.ListOptions{
FieldSelector: fields.OneTermEqualSelector("metadata.name", resourceQuota.Name).String(),
Watch: true,
}

watchInterface, err := c.kubernetes.CoreV1().ResourceQuotas(resourceQuota.Namespace).Watch(context.TODO(), listOptions)
if err != nil {
logrus.WithError(err).Error("error invoking the watch api for resource quotas")
return err
}

// fire off watch function
go c.handleWatch(watchInterface, resourceQuota, "", fn, listOptions)
return nil
}

// ListResourceQuota returns the list of ResourceQuotas
func (c *Client) ListResourceQuota(namespace string, filterOptions metav1.ListOptions) (*corev1.ResourceQuotaList, error) {
if err := c.initClient(); err != nil {
return nil, err
}

return c.kubernetes.CoreV1().ResourceQuotas(namespace).List(context.TODO(), filterOptions)
}