Skip to content

Commit

Permalink
🔨 fix: import consistent corev1
Browse files Browse the repository at this point in the history
  • Loading branch information
anngdinh committed Sep 18, 2024
1 parent b5181ca commit 578a355
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 82 deletions.
35 changes: 17 additions & 18 deletions pkg/ingress/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
"github.com/vngcloud/vngcloud-go-sdk/vngcloud/services/loadbalancer/v2/policy"
"github.com/vngcloud/vngcloud-go-sdk/vngcloud/services/loadbalancer/v2/pool"
"github.com/vngcloud/vngcloud-go-sdk/vngcloud/services/network/v2/extensions/secgroup_rule"
apiv1 "k8s.io/api/core/v1"
corev1 "k8s.io/api/core/v1"
nwv1 "k8s.io/api/networking/v1"
apimetav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -73,7 +72,7 @@ type Controller struct {
kubeClient kubernetes.Interface

stopCh chan struct{}
knownNodes []*apiv1.Node
knownNodes []*corev1.Node
queue workqueue.RateLimitingInterface
informer informers.SharedInformerFactory
recorder record.EventRecorder
Expand Down Expand Up @@ -126,7 +125,7 @@ func NewController(conf config.Config) *Controller {
eventBroadcaster.StartRecordingToSink(&v1core.EventSinkImpl{
Interface: kubeClient.CoreV1().Events(""),
})
recorder := eventBroadcaster.NewRecorder(scheme.Scheme, apiv1.EventSource{Component: "vngcloud-ingress-controller"})
recorder := eventBroadcaster.NewRecorder(scheme.Scheme, corev1.EventSource{Component: "vngcloud-ingress-controller"})

controller := &Controller{
config: &conf,
Expand All @@ -140,7 +139,7 @@ func NewController(conf config.Config) *Controller {
serviceListerSynced: serviceInformer.Informer().HasSynced,
nodeLister: nodeInformer.Lister(),
nodeListerSynced: nodeInformer.Informer().HasSynced,
knownNodes: []*apiv1.Node{},
knownNodes: []*corev1.Node{},
trackLBUpdate: utils.NewUpdateTracker(),
numOfUpdatingThread: 0,
queues: make(map[string][]interface{}),
Expand All @@ -159,7 +158,7 @@ func NewController(conf config.Config) *Controller {
return
}

recorder.Event(addIng, apiv1.EventTypeNormal, "Creating", fmt.Sprintf("Ingress %s", key))
recorder.Event(addIng, corev1.EventTypeNormal, "Creating", fmt.Sprintf("Ingress %s", key))
controller.queue.AddRateLimited(Event{Obj: addIng, Type: CreateEvent, oldObj: nil})
},
UpdateFunc: func(old, new interface{}) {
Expand All @@ -179,13 +178,13 @@ func NewController(conf config.Config) *Controller {
validOld := IsValid(oldIng)
validCur := IsValid(newIng)
if !validOld && validCur {
recorder.Event(newIng, apiv1.EventTypeNormal, "Creating", fmt.Sprintf("Ingress %s", key))
recorder.Event(newIng, corev1.EventTypeNormal, "Creating", fmt.Sprintf("Ingress %s", key))
controller.queue.AddRateLimited(Event{Obj: newIng, Type: CreateEvent, oldObj: nil})
} else if validOld && !validCur {
recorder.Event(newIng, apiv1.EventTypeNormal, "Deleting", fmt.Sprintf("Ingress %s", key))
recorder.Event(newIng, corev1.EventTypeNormal, "Deleting", fmt.Sprintf("Ingress %s", key))
controller.queue.AddRateLimited(Event{Obj: newIng, Type: DeleteEvent, oldObj: nil})
} else if validCur && (!reflect.DeepEqual(newIng.Spec, oldIng.Spec) || !reflect.DeepEqual(newAnnotations, oldAnnotations)) {
recorder.Event(newIng, apiv1.EventTypeNormal, "Updating", fmt.Sprintf("Ingress %s", key))
recorder.Event(newIng, corev1.EventTypeNormal, "Updating", fmt.Sprintf("Ingress %s", key))
controller.queue.AddRateLimited(Event{Obj: newIng, Type: UpdateEvent, oldObj: oldIng})
} else {
return
Expand Down Expand Up @@ -213,7 +212,7 @@ func NewController(conf config.Config) *Controller {
return
}

recorder.Event(delIng, apiv1.EventTypeNormal, "Deleting", fmt.Sprintf("Ingress %s", key))
recorder.Event(delIng, corev1.EventTypeNormal, "Deleting", fmt.Sprintf("Ingress %s", key))
controller.queue.AddRateLimited(Event{Obj: delIng, Type: DeleteEvent, oldObj: nil})
},
})
Expand Down Expand Up @@ -483,36 +482,36 @@ func (c *Controller) processItem(event Event) {

if err := c.ensureIngress(oldIng, ing); err != nil {
utilruntime.HandleError(fmt.Errorf("failed to create vngcloud resources for ingress %s: %v", key, err))
c.recorder.Event(ing, apiv1.EventTypeWarning, "Failed", fmt.Sprintf("Failed to create vngcloud resources for ingress %s: %v", key, err))
c.recorder.Event(ing, corev1.EventTypeWarning, "Failed", fmt.Sprintf("Failed to create vngcloud resources for ingress %s: %v", key, err))
} else {
c.recorder.Event(ing, apiv1.EventTypeNormal, "Created", fmt.Sprintf("Ingress %s", key))
c.recorder.Event(ing, corev1.EventTypeNormal, "Created", fmt.Sprintf("Ingress %s", key))
}
case UpdateEvent:
logger.Info("updating ingress")

if err := c.ensureIngress(oldIng, ing); err != nil {
utilruntime.HandleError(fmt.Errorf("failed to update vngcloud resources for ingress %s: %v", key, err))
c.recorder.Event(ing, apiv1.EventTypeWarning, "Failed", fmt.Sprintf("Failed to update vngcloud resources for ingress %s: %v", key, err))
c.recorder.Event(ing, corev1.EventTypeWarning, "Failed", fmt.Sprintf("Failed to update vngcloud resources for ingress %s: %v", key, err))
} else {
c.recorder.Event(ing, apiv1.EventTypeNormal, "Updated", fmt.Sprintf("Ingress %s", key))
c.recorder.Event(ing, corev1.EventTypeNormal, "Updated", fmt.Sprintf("Ingress %s", key))
}
case DeleteEvent:
logger.Info("deleting ingress")

if err := c.deleteIngress(ing); err != nil {
utilruntime.HandleError(fmt.Errorf("failed to delete vngcloud resources for ingress %s: %v", key, err))
c.recorder.Event(ing, apiv1.EventTypeWarning, "Failed", fmt.Sprintf("Failed to delete vngcloud resources for ingress %s: %v", key, err))
c.recorder.Event(ing, corev1.EventTypeWarning, "Failed", fmt.Sprintf("Failed to delete vngcloud resources for ingress %s: %v", key, err))
} else {
c.recorder.Event(ing, apiv1.EventTypeNormal, "Deleted", fmt.Sprintf("Ingress %s", key))
c.recorder.Event(ing, corev1.EventTypeNormal, "Deleted", fmt.Sprintf("Ingress %s", key))
}
case SyncEvent:
logger.Info("sync ingress")

if err := c.ensureIngress(oldIng, ing); err != nil {
utilruntime.HandleError(fmt.Errorf("failed to sync vngcloud resources for ingress %s: %v", key, err))
c.recorder.Event(ing, apiv1.EventTypeWarning, "Failed", fmt.Sprintf("Failed to sync vngcloud resources for ingress %s: %v", key, err))
c.recorder.Event(ing, corev1.EventTypeWarning, "Failed", fmt.Sprintf("Failed to sync vngcloud resources for ingress %s: %v", key, err))
} else {
c.recorder.Event(ing, apiv1.EventTypeNormal, "Synced", fmt.Sprintf("Ingress %s", key))
c.recorder.Event(ing, corev1.EventTypeNormal, "Synced", fmt.Sprintf("Ingress %s", key))
}
}

Expand Down Expand Up @@ -586,7 +585,7 @@ func (c *Controller) updateIngressStatus(ing *nwv1.Ingress, lb *lObjects.LoadBal
if err != nil {
return nil, err
}
c.recorder.Event(ing, apiv1.EventTypeNormal, "Updated", fmt.Sprintf("Successfully associated IP address %s to ingress %s", lb.Address, newIng.Name))
c.recorder.Event(ing, corev1.EventTypeNormal, "Updated", fmt.Sprintf("Successfully associated IP address %s to ingress %s", lb.Address, newIng.Name))
return newObj, nil
}

Expand Down
14 changes: 7 additions & 7 deletions pkg/utils/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
log "github.com/sirupsen/logrus"
"github.com/vngcloud/cloud-provider-vngcloud/pkg/consts"
"github.com/vngcloud/cloud-provider-vngcloud/pkg/utils/errors"
apiv1 "k8s.io/api/core/v1"
corev1 "k8s.io/api/core/v1"
nwv1 "k8s.io/api/networking/v1"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/client-go/kubernetes"
Expand Down Expand Up @@ -60,7 +60,7 @@ func GetIngress(ingressLister nwlisters.IngressLister, key string) (*nwv1.Ingres
return ingress, nil
}

func GetService(serviceLister corelisters.ServiceLister, key string) (*apiv1.Service, error) {
func GetService(serviceLister corelisters.ServiceLister, key string) (*corev1.Service, error) {
namespace, name, err := cache.SplitMetaNamespaceKey(key)
if err != nil {
return nil, err
Expand Down Expand Up @@ -112,7 +112,7 @@ func GetServiceNodePort(serviceLister corelisters.ServiceLister, name string, se
return targetPort, nodePort, nil
}

func GetNodeMembersAddr(nodeObjs []*apiv1.Node) []string {
func GetNodeMembersAddr(nodeObjs []*corev1.Node) []string {
var nodeAddr []string
for _, node := range nodeObjs {
addr, err := getNodeAddressForLB(node)
Expand All @@ -126,14 +126,14 @@ func GetNodeMembersAddr(nodeObjs []*apiv1.Node) []string {
return nodeAddr
}

func getNodeAddressForLB(node *apiv1.Node) (string, error) {
func getNodeAddressForLB(node *corev1.Node) (string, error) {
addrs := node.Status.Addresses
if len(addrs) == 0 {
return "", errors.NewErrNodeAddressNotFound(node.Name, "")
}

for _, addr := range addrs {
if addr.Type == apiv1.NodeInternalIP {
if addr.Type == corev1.NodeInternalIP {
return addr.Address, nil
}
}
Expand All @@ -142,7 +142,7 @@ func getNodeAddressForLB(node *apiv1.Node) (string, error) {
}

// NodeNames get all the node names.
func NodeNames(nodes []*apiv1.Node) []string {
func NodeNames(nodes []*corev1.Node) []string {
ret := make([]string, len(nodes))
for i, node := range nodes {
ret[i] = node.Name
Expand All @@ -151,7 +151,7 @@ func NodeNames(nodes []*apiv1.Node) []string {
}

// NodeSlicesEqual check if two nodes equals to each other.
func NodeSlicesEqual(x, y []*apiv1.Node) bool {
func NodeSlicesEqual(x, y []*corev1.Node) bool {
if len(x) != len(y) {
return false
}
Expand Down
46 changes: 23 additions & 23 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
lListenerV2 "github.com/vngcloud/vngcloud-go-sdk/vngcloud/services/loadbalancer/v2/listener"
lLoadBalancerV2 "github.com/vngcloud/vngcloud-go-sdk/vngcloud/services/loadbalancer/v2/loadbalancer"
lPoolV2 "github.com/vngcloud/vngcloud-go-sdk/vngcloud/services/loadbalancer/v2/pool"
apiv1 "k8s.io/api/core/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/types"
Expand All @@ -32,7 +32,7 @@ type MyDuration struct {
}

// PatchService makes patch request to the Service object.
func PatchService(ctx context.Context, client clientset.Interface, cur, mod *apiv1.Service) error {
func PatchService(ctx context.Context, client clientset.Interface, cur, mod *corev1.Service) error {
curJSON, err := json.Marshal(cur)
if err != nil {
return fmt.Errorf("failed to serialize current service object: %s", err)
Expand All @@ -43,7 +43,7 @@ func PatchService(ctx context.Context, client clientset.Interface, cur, mod *api
return fmt.Errorf("failed to serialize modified service object: %s", err)
}

patch, err := strategicpatch.CreateTwoWayMergePatch(curJSON, modJSON, apiv1.Service{})
patch, err := strategicpatch.CreateTwoWayMergePatch(curJSON, modJSON, corev1.Service{})
if err != nil {
return fmt.Errorf("failed to create 2-way merge patch: %s", err)
}
Expand All @@ -60,7 +60,7 @@ func PatchService(ctx context.Context, client clientset.Interface, cur, mod *api
return nil
}

func IsPoolProtocolValid(pPool *lObjects.Pool, pPort apiv1.ServicePort, pPoolName string) bool {
func IsPoolProtocolValid(pPool *lObjects.Pool, pPort corev1.ServicePort, pPoolName string) bool {
if pPool != nil &&
!lStr.EqualFold(lStr.TrimSpace(pPool.Protocol), lStr.TrimSpace(string(pPort.Protocol))) &&
pPoolName == pPool.Name {
Expand All @@ -77,8 +77,8 @@ func MinInt(a, b int) int {
return b
}

func ListWorkerNodes(pNodes []*apiv1.Node, pOnlyReadyNode bool) []*apiv1.Node {
var workerNodes []*apiv1.Node
func ListWorkerNodes(pNodes []*corev1.Node, pOnlyReadyNode bool) []*corev1.Node {
var workerNodes []*corev1.Node

for _, node := range pNodes {
// Ignore master nodes
Expand All @@ -99,7 +99,7 @@ func ListWorkerNodes(pNodes []*apiv1.Node, pOnlyReadyNode bool) []*apiv1.Node {

// Only consider ready nodes
for _, condition := range node.Status.Conditions {
if condition.Type == apiv1.NodeReady && condition.Status != apiv1.ConditionTrue {
if condition.Type == corev1.NodeReady && condition.Status != corev1.ConditionTrue {
continue
}
}
Expand Down Expand Up @@ -136,7 +136,7 @@ func ParsePoolProtocol(pPoolProtocol string) lPoolV2.CreateOptsProtocolOpt {
}

func ParseMonitorProtocol(
pPoolProtocol apiv1.Protocol, pMonitorProtocol string) lPoolV2.CreateOptsHealthCheckProtocolOpt {
pPoolProtocol corev1.Protocol, pMonitorProtocol string) lPoolV2.CreateOptsHealthCheckProtocolOpt {

switch lStr.TrimSpace(lStr.ToUpper(string(pPoolProtocol))) {
case string(lPoolV2.CreateOptsProtocolOptUDP):
Expand Down Expand Up @@ -189,7 +189,7 @@ func ParseLoadBalancerScheme(pInternal bool) lLoadBalancerV2.CreateOptsSchemeOpt
return lLoadBalancerV2.CreateOptsSchemeOptInternet
}

func ParseListenerProtocol(pPort apiv1.ServicePort) lListenerV2.CreateOptsListenerProtocolOpt {
func ParseListenerProtocol(pPort corev1.ServicePort) lListenerV2.CreateOptsListenerProtocolOpt {
opt := lStr.TrimSpace(lStr.ToUpper(string(pPort.Protocol)))
switch opt {
case string(lListenerV2.CreateOptsListenerProtocolOptUDP):
Expand All @@ -199,7 +199,7 @@ func ParseListenerProtocol(pPort apiv1.ServicePort) lListenerV2.CreateOptsListen
return lListenerV2.CreateOptsListenerProtocolOptTCP
}

func GetStringFromServiceAnnotation(pService *apiv1.Service, annotationKey string, defaultSetting string) string {
func GetStringFromServiceAnnotation(pService *corev1.Service, annotationKey string, defaultSetting string) string {
klog.V(4).Infof("getStringFromServiceAnnotation(%s/%s, %v, %v)", pService.Namespace, pService.Name, annotationKey, defaultSetting)
if annotationValue, ok := pService.Annotations[annotationKey]; ok {
//if there is an annotation for this setting, set the "setting" var to it
Expand All @@ -217,7 +217,7 @@ func GetStringFromServiceAnnotation(pService *apiv1.Service, annotationKey strin
return defaultSetting
}

func GetIntFromServiceAnnotation(service *apiv1.Service, annotationKey string, defaultSetting int) int {
func GetIntFromServiceAnnotation(service *corev1.Service, annotationKey string, defaultSetting int) int {
klog.V(4).Infof("getIntFromServiceAnnotation(%s/%s, %v, %v)", service.Namespace, service.Name, annotationKey, defaultSetting)
if annotationValue, ok := service.Annotations[annotationKey]; ok {
returnValue, err := strconv.Atoi(annotationValue)
Expand Down Expand Up @@ -297,14 +297,14 @@ func ParseStringMapAnnotation(s, annotation string) map[string]string {
return validStr
}

func ListNodeWithPredicate(nodeLister corelisters.NodeLister, nodeLabels map[string]string) ([]*apiv1.Node, error) {
func ListNodeWithPredicate(nodeLister corelisters.NodeLister, nodeLabels map[string]string) ([]*corev1.Node, error) {
labelSelector := labels.SelectorFromSet(nodeLabels)
nodes, err := nodeLister.List(labelSelector)
if err != nil {
return nil, err
}

var filtered []*apiv1.Node
var filtered []*corev1.Node
for i := range nodes {
if getNodeConditionPredicate(nodes[i]) {
filtered = append(filtered, nodes[i])
Expand All @@ -315,8 +315,8 @@ func ListNodeWithPredicate(nodeLister corelisters.NodeLister, nodeLabels map[str
}

// FilterByNodeLabel filters the given list of nodes by the given node labels.
func FilterByNodeLabel(nodes []*apiv1.Node, nodeLabels map[string]string) []*apiv1.Node {
var filtered []*apiv1.Node
func FilterByNodeLabel(nodes []*corev1.Node, nodeLabels map[string]string) []*corev1.Node {
var filtered []*corev1.Node
for _, node := range nodes {
if node == nil {
continue
Expand All @@ -331,7 +331,7 @@ func FilterByNodeLabel(nodes []*apiv1.Node, nodeLabels map[string]string) []*api
return filtered
}

func getNodeConditionPredicate(node *apiv1.Node) bool {
func getNodeConditionPredicate(node *corev1.Node) bool {
// We add the master to the node list, but its unschedulable. So we use this to filter
// the master.
if node.Spec.Unschedulable {
Expand All @@ -355,21 +355,21 @@ func getNodeConditionPredicate(node *apiv1.Node) bool {
// for _, cond := range node.Status.Conditions {
// // We consider the node for load balancing only when its NodeReady condition status
// // is ConditionTrue
// if cond.Type == apiv1.NodeReady && cond.Status != apiv1.ConditionTrue {
// if cond.Type == corev1.NodeReady && cond.Status != corev1.ConditionTrue {
// klog.Info("ignoring node:", "name", node.Name, "status", cond.Status)
// // log.WithFields(log.Fields{"name": node.Name, "status": cond.Status}).Info("ignoring node")
// return false
// }
// }
return true
}
func ListServiceWithPredicate(serviceLister corelisters.ServiceLister) ([]*apiv1.Service, error) {
func ListServiceWithPredicate(serviceLister corelisters.ServiceLister) ([]*corev1.Service, error) {
services, err := serviceLister.List(labels.Everything())
if err != nil {
return nil, err
}

var filtered []*apiv1.Service
var filtered []*corev1.Service
for i := range services {
if getServiceConditionPredicate(services[i]) {
filtered = append(filtered, services[i])
Expand All @@ -378,9 +378,9 @@ func ListServiceWithPredicate(serviceLister corelisters.ServiceLister) ([]*apiv1

return filtered, nil
}
func getServiceConditionPredicate(service *apiv1.Service) bool {
func getServiceConditionPredicate(service *corev1.Service) bool {
// We only consider services with type LoadBalancer
return service.Spec.Type == apiv1.ServiceTypeLoadBalancer
return service.Spec.Type == corev1.ServiceTypeLoadBalancer
}

// providerID
Expand All @@ -395,7 +395,7 @@ var (
vngCloudProviderIDRegex = regexp.MustCompile(pattern)
)

func GetListProviderID(pnodes []*apiv1.Node) []string {
func GetListProviderID(pnodes []*corev1.Node) []string {
var providerIDs []string
for _, node := range pnodes {
if node != nil && (matchCloudProviderPattern(node.Spec.ProviderID)) {
Expand All @@ -410,7 +410,7 @@ func matchCloudProviderPattern(pproviderID string) bool {
return vngCloudProviderIDRegex.MatchString(pproviderID)
}

func getProviderID(pnode *apiv1.Node) string {
func getProviderID(pnode *corev1.Node) string {
return pnode.Spec.ProviderID[len(rawPrefix):len(pnode.Spec.ProviderID)]
}

Expand Down
Loading

0 comments on commit 578a355

Please sign in to comment.