Skip to content

Commit

Permalink
deepcopy fix steps
Browse files Browse the repository at this point in the history
  • Loading branch information
lut777 committed Nov 18, 2021
1 parent 5518218 commit c9f5f4b
Show file tree
Hide file tree
Showing 14 changed files with 64 additions and 36 deletions.
5 changes: 3 additions & 2 deletions pkg/controller/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,14 @@ func (c *Controller) handleUpdateEndpoint(key string) error {
return err
}

svc, err := c.servicesLister.Services(namespace).Get(name)
orisvc, err := c.servicesLister.Services(namespace).Get(name)
if err != nil {
if errors.IsNotFound(err) {
return nil
}
return err
}
svc := orisvc.DeepCopy()

clusterIPs := svc.Spec.ClusterIPs
if len(clusterIPs) == 0 && svc.Spec.ClusterIP != "" && svc.Spec.ClusterIP != v1.ClusterIPNone {
Expand Down Expand Up @@ -168,7 +169,7 @@ func (c *Controller) handleUpdateEndpoint(key string) error {
svc.Annotations = make(map[string]string, 1)
}
svc.Annotations[util.VpcAnnotation] = vpcName
if svc, err = c.config.KubeClient.CoreV1().Services(namespace).Update(context.Background(), svc, metav1.UpdateOptions{}); err != nil {
if _, err = c.config.KubeClient.CoreV1().Services(namespace).Update(context.Background(), svc, metav1.UpdateOptions{}); err != nil {
klog.Errorf("failed to update service %s/%s: %v", namespace, svc.Name, err)
return err
}
Expand Down
6 changes: 4 additions & 2 deletions pkg/controller/external-gw.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ func (c *Controller) removeExternalGateway() error {
klog.Errorf("failed to list nodes, %v", err)
return err
}
for _, no := range nodes {
for _, orino := range nodes {
no := orino.DeepCopy()
patchPayloadTemplate :=
`[{
"op": "%s",
Expand Down Expand Up @@ -116,11 +117,12 @@ func (c *Controller) establishExternalGateway(config map[string]string) error {
}
for _, gw := range gwNodes {
gw = strings.TrimSpace(gw)
node, err := c.nodesLister.Get(gw)
orinode, err := c.nodesLister.Get(gw)
if err != nil {
klog.Errorf("failed to get gw node %s, %v", gw, err)
return err
}
node := orinode.DeepCopy()
patchPayloadTemplate :=
`[{
"op": "%s",
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/external_vpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func (c *Controller) syncExternalVpc() {
}
vpcMaps := make(map[string]*v1.Vpc)
for _, vpc := range vpcs {
vpcMaps[vpc.Name] = vpc
vpcMaps[vpc.Name] = vpc.DeepCopy()
}
for vpcName, vpc := range vpcMaps {
if _, ok := logicalRouters[vpcName]; ok {
Expand Down
3 changes: 2 additions & 1 deletion pkg/controller/gc.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,8 @@ func (c *Controller) gcLoadBalancer() error {
if err != nil {
return err
}
for _, vpc := range vpcs {
for _, orivpc := range vpcs {
vpc := orivpc.DeepCopy()
for _, subnetName := range vpc.Status.Subnets {
_, err := c.subnetsLister.Get(subnetName)
if err != nil {
Expand Down
22 changes: 13 additions & 9 deletions pkg/controller/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,17 @@ func (c *Controller) InitOVN() error {
}

func (c *Controller) InitDefaultVpc() error {
vpc, err := c.vpcsLister.Get(util.DefaultVpc)
orivpc, err := c.vpcsLister.Get(util.DefaultVpc)
if err != nil {
vpc = &kubeovnv1.Vpc{}
vpc.Name = util.DefaultVpc
vpc, err = c.config.KubeOvnClient.KubeovnV1().Vpcs().Create(context.Background(), vpc, metav1.CreateOptions{})
orivpc = &kubeovnv1.Vpc{}
orivpc.Name = util.DefaultVpc
orivpc, err = c.config.KubeOvnClient.KubeovnV1().Vpcs().Create(context.Background(), orivpc, metav1.CreateOptions{})
if err != nil {
klog.Errorf("init default vpc failed: %v", err)
return err
}
}

vpc := orivpc.DeepCopy()
vpc.Status.DefaultLogicalSwitch = c.config.DefaultLogicalSwitch
vpc.Status.Router = c.config.ClusterRouter
if c.config.EnableLb {
Expand Down Expand Up @@ -95,6 +95,7 @@ func (c *Controller) initDefaultLogicalSwitch() error {
if subnet != nil && util.CheckProtocol(c.config.DefaultCIDR) != util.CheckProtocol(subnet.Spec.CIDRBlock) {
// single-stack upgrade to dual-stack
if util.CheckProtocol(c.config.DefaultCIDR) == kubeovnv1.ProtocolDual {
subnet := subnet.DeepCopy()
subnet.Spec.CIDRBlock = c.config.DefaultCIDR
if err := formatSubnet(subnet, c); err != nil {
klog.Errorf("init format subnet %s failed: %v", c.config.DefaultLogicalSwitch, err)
Expand Down Expand Up @@ -141,6 +142,7 @@ func (c *Controller) initNodeSwitch() error {
if subnet != nil && util.CheckProtocol(c.config.NodeSwitchCIDR) != util.CheckProtocol(subnet.Spec.CIDRBlock) {
// single-stack upgrade to dual-stack
if util.CheckProtocol(c.config.NodeSwitchCIDR) == kubeovnv1.ProtocolDual {
subnet := subnet.DeepCopy()
subnet.Spec.CIDRBlock = c.config.NodeSwitchCIDR
if err := formatSubnet(subnet, c); err != nil {
klog.Errorf("init format subnet %s failed: %v", c.config.NodeSwitch, err)
Expand Down Expand Up @@ -197,7 +199,8 @@ func (c *Controller) initLoadBalancer() error {
return err
}

for _, vpc := range vpcs {
for _, orivpc := range vpcs {
vpc := orivpc.DeepCopy()
vpcLb := c.GenVpcLoadBalancer(vpc.Name)

tcpLb, err := c.ovnClient.FindLoadbalancer(vpcLb.TcpLoadBalancer)
Expand Down Expand Up @@ -438,15 +441,15 @@ func (c *Controller) initSyncCrdIPs() error {
}

for _, ipCr := range ips.Items {
ip := ipCr
ip := ipCr.DeepCopy()
v4IP, v6IP := util.SplitStringIP(ip.Spec.IPAddress)
if ip.Spec.V4IPAddress == v4IP && ip.Spec.V6IPAddress == v6IP {
continue
}
ip.Spec.V4IPAddress = v4IP
ip.Spec.V6IPAddress = v6IP

_, err := c.config.KubeOvnClient.KubeovnV1().IPs().Update(context.Background(), &ip, metav1.UpdateOptions{})
_, err := c.config.KubeOvnClient.KubeovnV1().IPs().Update(context.Background(), ip, metav1.UpdateOptions{})
if err != nil {
klog.Errorf("failed to sync crd ip %s: %v", ip.Spec.IPAddress, err)
return err
Expand All @@ -464,7 +467,8 @@ func (c *Controller) initSyncCrdSubnets() error {
}
return err
}
for _, subnet := range subnets {
for _, orisubnet := range subnets {
subnet := orisubnet.DeepCopy()
if util.CheckProtocol(subnet.Spec.CIDRBlock) == kubeovnv1.ProtocolDual {
err = calcDualSubnetStatusIP(subnet, c)
} else {
Expand Down
3 changes: 2 additions & 1 deletion pkg/controller/inspection.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ func (c *Controller) inspectPod() error {
klog.Errorf("failed to list logical switch port, %v", err)
return err
}
for _, pod := range pods {
for _, oripod := range pods {
pod := oripod.DeepCopy()
if pod.Spec.HostNetwork {
continue
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/controller/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,14 @@ func (c *Controller) processNextAddNamespaceWorkItem() bool {
}

func (c *Controller) handleAddNamespace(key string) error {
namespace, err := c.namespacesLister.Get(key)
orinamespace, err := c.namespacesLister.Get(key)
if err != nil {
if errors.IsNotFound(err) {
return nil
}
return err
}
namespace := orinamespace.DeepCopy()

var ls, cidr string
var excludeIps []string
Expand Down
6 changes: 4 additions & 2 deletions pkg/controller/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,13 +194,14 @@ func nodeUnderlayAddressSetName(node string, af int) string {
}

func (c *Controller) handleAddNode(key string) error {
node, err := c.nodesLister.Get(key)
orinode, err := c.nodesLister.Get(key)
if err != nil {
if k8serrors.IsNotFound(err) {
return nil
}
return err
}
node := orinode.DeepCopy()

subnets, err := c.subnetsLister.List(labels.Everything())
if err != nil {
Expand Down Expand Up @@ -490,7 +491,8 @@ func (c *Controller) handleUpdateNode(key string) error {
return err
}

for _, subnet := range subnets {
for _, orisubnet := range subnets {
subnet := orisubnet.DeepCopy()
if util.GatewayContains(subnet.Spec.GatewayNode, node.Name) {
if err := c.reconcileGateway(subnet); err != nil {
return err
Expand Down
6 changes: 4 additions & 2 deletions pkg/controller/ovn-ic.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ func (c *Controller) removeInterConnection(azName string) error {
klog.Errorf("failed to list nodes, %v", err)
return err
}
for _, no := range nodes {
for _, orino := range nodes {
no := orino.DeepCopy()
patchPayloadTemplate :=
`[{
"op": "%s",
Expand Down Expand Up @@ -174,11 +175,12 @@ func (c *Controller) establishInterConnection(config map[string]string) error {
gwNodes := strings.Split(config["gw-nodes"], ",")
for _, gw := range gwNodes {
gw = strings.TrimSpace(gw)
node, err := c.nodesLister.Get(gw)
orinode, err := c.nodesLister.Get(gw)
if err != nil {
klog.Errorf("failed to get gw node %s, %v", gw, err)
return err
}
node := orinode.DeepCopy()
patchPayloadTemplate :=
`[{
"op": "%s",
Expand Down
6 changes: 4 additions & 2 deletions pkg/controller/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -413,13 +413,14 @@ func (c *Controller) handleAddPod(key string) error {
utilruntime.HandleError(fmt.Errorf("invalid resource key: %s", key))
return nil
}
pod, err := c.podsLister.Pods(namespace).Get(name)
oripod, err := c.podsLister.Pods(namespace).Get(name)
if err != nil {
if k8serrors.IsNotFound(err) {
return nil
}
return err
}
pod := oripod.DeepCopy()
if err := util.ValidatePodNetwork(pod.Annotations); err != nil {
klog.Errorf("validate pod %s/%s failed: %v", namespace, name, err)
c.recorder.Eventf(pod, v1.EventTypeWarning, "ValidatePodNetworkFailed", err.Error())
Expand Down Expand Up @@ -668,13 +669,14 @@ func (c *Controller) handleUpdatePod(key string) error {
utilruntime.HandleError(fmt.Errorf("invalid resource key: %s", key))
return nil
}
pod, err := c.podsLister.Pods(namespace).Get(name)
oripod, err := c.podsLister.Pods(namespace).Get(name)
if err != nil {
if k8serrors.IsNotFound(err) {
return nil
}
return err
}
pod := oripod.DeepCopy()

// in case update handler overlap the annotation when cache is not in sync
if pod.Annotations[util.AllocatedAnnotation] == "" {
Expand Down
3 changes: 2 additions & 1 deletion pkg/controller/security_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,13 +212,14 @@ func (c *Controller) handleAddOrUpdateSg(key string) error {
}
}

sg, err := c.sgsLister.Get(key)
orisg, err := c.sgsLister.Get(key)
if err != nil {
if k8serrors.IsNotFound(err) {
return nil
}
return err
}
sg := orisg.DeepCopy()

if err = c.validateSgRule(sg); err != nil {
return err
Expand Down
11 changes: 7 additions & 4 deletions pkg/controller/subnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -446,13 +446,14 @@ func (c *Controller) handleAddOrUpdateSubnet(key string) error {
return nil
}

subnet, err = c.subnetsLister.Get(key)
orisubnet, err := c.subnetsLister.Get(key)
if err != nil {
if k8serrors.IsNotFound(err) {
return nil
}
return err
}
subnet = orisubnet.DeepCopy()

if err := formatSubnet(subnet, c); err != nil {
return err
Expand Down Expand Up @@ -677,7 +678,8 @@ func (c *Controller) updateNodeAddressSetsForSubnet(subnet *kubeovnv1.Subnet, de
}

func (c *Controller) handleUpdateSubnetStatus(key string) error {
subnet, err := c.subnetsLister.Get(key)
orisubnet, err := c.subnetsLister.Get(key)
subnet := orisubnet.DeepCopy()
if err != nil {
if k8serrors.IsNotFound(err) {
return nil
Expand Down Expand Up @@ -851,10 +853,11 @@ func (c *Controller) reconcileNamespaces(subnet *kubeovnv1.Subnet) error {
namespaceMap[ns] = true
}

for _, sub := range subnets {
if sub.Name == subnet.Name || len(sub.Spec.Namespaces) == 0 {
for _, orisub := range subnets {
if orisub.Name == subnet.Name || len(orisub.Spec.Namespaces) == 0 {
continue
}
sub := orisub.DeepCopy()

changed := false
reservedNamespaces := []string{}
Expand Down
6 changes: 4 additions & 2 deletions pkg/controller/vpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,14 @@ func (c *Controller) handleDelVpc(vpc *kubeovnv1.Vpc) error {
}

func (c *Controller) handleUpdateVpcStatus(key string) error {
vpc, err := c.vpcsLister.Get(key)
orivpc, err := c.vpcsLister.Get(key)
if err != nil {
if k8serrors.IsNotFound(err) {
return nil
}
return err
}
vpc := orivpc.DeepCopy()

subnets, defaultSubnet, err := c.getVpcSubnets(vpc)
if err != nil {
Expand Down Expand Up @@ -251,13 +252,14 @@ func (c *Controller) addLoadBalancer(vpc string) (*VpcLoadBalancer, error) {
}

func (c *Controller) handleAddOrUpdateVpc(key string) error {
vpc, err := c.vpcsLister.Get(key)
orivpc, err := c.vpcsLister.Get(key)
if err != nil {
if k8serrors.IsNotFound(err) {
return nil
}
return err
}
vpc := orivpc.DeepCopy()

if err = formatVpc(vpc, c); err != nil {
klog.Errorf("failed to format vpc: %v", err)
Expand Down

0 comments on commit c9f5f4b

Please sign in to comment.