Skip to content

Commit

Permalink
Merge pull request #90 from qiangzii/master
Browse files Browse the repository at this point in the history
fix node patch
  • Loading branch information
liangcd authored Oct 13, 2023
2 parents 27f44d0 + b9f52a1 commit c11a1be
Show file tree
Hide file tree
Showing 13 changed files with 176 additions and 33 deletions.
4 changes: 2 additions & 2 deletions cmd/controller/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,10 @@ func main() {
k8sInformerFactory := k8sinformers.NewSharedInformerFactory(k8sClient, time.Second*30)
informerFactory := informers.NewSharedInformerFactory(client, time.Second*30)

c1 := controller.NewVxNetPoolController(clusterConfig, k8sClient, client, informerFactory)
c1 := controller.NewVxNetPoolController(clusterConfig, k8sClient, client, informerFactory, k8sInformerFactory)

c2 := controller.NewIPPoolController(k8sClient, client,
k8sInformerFactory, informerFactory, ippool.NewProvider(client, networkv1alpha1.IPPoolTypeLocal, informerFactory))
k8sInformerFactory, informerFactory, ippool.NewProvider(client, networkv1alpha1.IPPoolTypeLocal, informerFactory, k8sInformerFactory))

// notice that there is no need to run Start methods in a separate goroutine. (i.e. go kubeInformerFactory.Start(stopCh)
// Start method is non-blocking and runs all registered informers in a dedicated goroutine.
Expand Down
2 changes: 1 addition & 1 deletion cmd/ipam/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func main() {
informerFactory := informers.NewSharedInformerFactory(client, time.Second*30)

clusterConfig := config.NewClusterConfig(k8sInformerFactory.Core().V1().ConfigMaps())
ipamClient := ipam.NewIPAMClient(client, networkv1alpha1.IPPoolTypeLocal, informerFactory)
ipamClient := ipam.NewIPAMClient(client, networkv1alpha1.IPPoolTypeLocal, informerFactory, k8sInformerFactory)

k8sInformerFactory.Start(stopCh)
informerFactory.Start(stopCh)
Expand Down
14 changes: 9 additions & 5 deletions cmd/tools/ipam-client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ import (
"fmt"
"time"

networkv1alpha1 "github.com/yunify/hostnic-cni/pkg/apis/network/v1alpha1"
"github.com/yunify/hostnic-cni/pkg/signals"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
k8sinformers "k8s.io/client-go/informers"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"

networkv1alpha1 "github.com/yunify/hostnic-cni/pkg/apis/network/v1alpha1"
clientset "github.com/yunify/hostnic-cni/pkg/client/clientset/versioned"
informers "github.com/yunify/hostnic-cni/pkg/client/informers/externalversions"
"github.com/yunify/hostnic-cni/pkg/constants"
Expand Down Expand Up @@ -49,20 +50,23 @@ func main() {
return
}

k8sInformerFactory := k8sinformers.NewSharedInformerFactory(k8sClient, time.Second*10)
informerFactory := informers.NewSharedInformerFactory(client, time.Second*30)
ipamClient := ipam.NewIPAMClient(client, networkv1alpha1.IPPoolTypeLocal, informerFactory)

ipamClient := ipam.NewIPAMClient(client, networkv1alpha1.IPPoolTypeLocal, informerFactory, k8sInformerFactory)

k8sInformerFactory.Start(stopCh)
informerFactory.Start(stopCh)

if err = ipamClient.Sync(stopCh); err != nil {
fmt.Printf("ipamclient sync error: %v", err)
return
}

c := ipam.NewIPAMClient(client, networkv1alpha1.IPPoolTypeLocal, informerFactory)
// c := ipam.NewIPAMClient(client, networkv1alpha1.IPPoolTypeLocal, informerFactory)

if handleID != "" {
if err := c.ReleaseByHandle(handleID); err != nil {
if err := ipamClient.ReleaseByHandle(handleID); err != nil {
fmt.Printf("Release %s failed: %v\n", handleID, err)
} else {
fmt.Printf("Release %s OK\n", handleID)
Expand All @@ -73,7 +77,7 @@ func main() {
if pool != "" {
args.Pools = []string{pool}
}
utils, err := c.GetPoolBlocksUtilization(args)
utils, err := ipamClient.GetPoolBlocksUtilization(args)
if err != nil {
fmt.Printf("GetUtilization failed: %v\n", err)
return
Expand Down
21 changes: 17 additions & 4 deletions cmd/tools/node-patch/patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ package main
import (
"context"
"flag"
"strings"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/klog/v2"

"github.com/yunify/hostnic-cni/pkg/conf"
"github.com/yunify/hostnic-cni/pkg/constants"
"github.com/yunify/hostnic-cni/pkg/qcclient"
"github.com/yunify/hostnic-cni/pkg/rpc"
)
Expand Down Expand Up @@ -60,9 +63,11 @@ func handleNode(clusterID string, k8sClient *kubernetes.Clientset) error {
if host := getHostForNode(node.Status.Addresses, qcNodes); host != "" {
if err := patchNode(k8sClient, host, node); err != nil {
klog.Errorf("patch %s failed: %v", topoKey, err)
} else {
klog.Infof("patch node %s with hostmachine %s success", node.Name, host)
}
} else {
klog.Errorf("get host for %s failed", node.Name)
klog.Errorf("get host for node %s failed", node.Name)
}
}
return nil
Expand All @@ -76,9 +81,17 @@ func main() {
flag.StringVar(&clusterID, "clusterID", "", "clusterID")
flag.Parse()

if clusterID == "" {
klog.Info("Plesse input clusterID: ./patch-node --clusterID cl-xxxxxxxx")
return
if !strings.HasPrefix(clusterID, "cl-") {
// use clusterid read from /etc/kubernetes/qingcloud.yaml
klog.Infof("invalid clusterID %s, try to get clusterID from volumed cluterConfig %s", clusterID, constants.DefaultClusterConfigPath)

clusterConfig, err := conf.TryLoadClusterConfFromDisk(constants.DefaultClusterConfigPath)
if err != nil || clusterConfig == nil {
klog.Fatalf("Error building clusterConfig: %s", err.Error())
}
clusterID = clusterConfig.ClusterID
klog.Infof("get clusterID success: %s", clusterID)

}

// setup qcclient, k8s
Expand Down
4 changes: 2 additions & 2 deletions config/default/manager_image_patch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ spec:
template:
spec:
initContainers:
- image: qingcloud/hostnic-plus:v1.0.3
- image: qingcloud/hostnic-plus:v1.0.7
name: hostnic-init
imagePullPolicy: IfNotPresent
containers:
# Change the value of image field below to your controller image URL
- image: qingcloud/hostnic-plus:v1.0.3
- image: qingcloud/hostnic-plus:v1.0.7
name: hostnic-node
imagePullPolicy: IfNotPresent
4 changes: 2 additions & 2 deletions config/default/manager_image_patch.yaml-e
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ spec:
template:
spec:
initContainers:
- image: qingcloud/hostnic-plus:v1.0.3
- image: qingcloud/hostnic-plus:v1.0.7
name: hostnic-init
imagePullPolicy: IfNotPresent
containers:
# Change the value of image field below to your controller image URL
- image: qingcloud/hostnic-plus:v1.0.3
- image: qingcloud/hostnic-plus:v1.0.7
name: hostnic-node
imagePullPolicy: IfNotPresent
10 changes: 8 additions & 2 deletions deploy/cronjob.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,18 @@ spec:
spec:
containers:
- name: node-patch
image: qingcloud/hostnic-plus:v1.0.0
command: ["sh", "-c", "/app/tools/patch-node --clusterID CLUSTERID"]
image: qingcloud/hostnic-plus:v1.0.7
command: ["sh", "-c", "/app/tools/patch-node"]
volumeMounts:
- mountPath: /root/.qingcloud/
name: apiaccesskey
readOnly: true
- mountPath: /etc/qingcloud/
name: qingcloud-cfg
readOnly: true
- mountPath: /etc/kubernetes
name: clusterconfig
readOnly: true
restartPolicy: OnFailure
serviceAccount: hostnic-node
serviceAccountName: hostnic-node
Expand All @@ -35,3 +38,6 @@ spec:
- hostPath:
path: /etc/qingcloud
name: qingcloud-cfg
- configMap:
name: clusterconfig
name: clusterconfig
6 changes: 3 additions & 3 deletions deploy/hostnic.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ spec:
fieldRef:
apiVersion: v1
fieldPath: metadata.namespace
image: qingcloud/hostnic-plus:v1.0.3
image: qingcloud/hostnic-plus:v1.0.7
imagePullPolicy: IfNotPresent
name: hostnic-node
ports:
Expand Down Expand Up @@ -513,7 +513,7 @@ spec:
- /app/install_hostnic.sh
command:
- /bin/sh
image: qingcloud/hostnic-plus:v1.0.3
image: qingcloud/hostnic-plus:v1.0.7
imagePullPolicy: IfNotPresent
name: hostnic-init
resources: {}
Expand Down Expand Up @@ -588,7 +588,7 @@ spec:
spec:
containers:
- name: hostnic-controller
image: qingcloud/hostnic-plus:v1.0.3
image: qingcloud/hostnic-plus:v1.0.7
command:
- /app/hostnic-controller
- --v=5
Expand Down
4 changes: 2 additions & 2 deletions pkg/conf/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package conf

import (
"fmt"
"io/ioutil"
"os"

"github.com/spf13/viper"
"k8s.io/apimachinery/pkg/util/yaml"
Expand Down Expand Up @@ -96,7 +96,7 @@ type ClusterConfig struct {
}

func TryLoadClusterConfFromDisk(file string) (*ClusterConfig, error) {
content, err := ioutil.ReadFile(file)
content, err := os.ReadFile(file)
if err != nil {
return nil, err
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/controller/vxnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"k8s.io/apimachinery/pkg/selection"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/apimachinery/pkg/util/wait"
k8sinformers "k8s.io/client-go/informers"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/tools/cache"
Expand Down Expand Up @@ -96,8 +97,7 @@ func NewVxNetPoolController(
kubeclientset kubernetes.Interface,
clientset clientset.Interface,
informers informers.SharedInformerFactory,
// ippoolInformer networkinformers.IPPoolInformer,
// poolInformer networkinformers.VxNetPoolInformer,
k8sInformers k8sinformers.SharedInformerFactory,
) *VxNetPoolController {

utilruntime.Must(poolscheme.AddToScheme(scheme.Scheme))
Expand All @@ -109,7 +109,7 @@ func NewVxNetPoolController(
controller := &VxNetPoolController{
kubeclientset: kubeclientset,
clientset: clientset,
ipamClient: ipam.NewIPAMClient(clientset, networkv1alpha1.IPPoolTypeLocal, informers),
ipamClient: ipam.NewIPAMClient(clientset, networkv1alpha1.IPPoolTypeLocal, informers, k8sInformers),
ippoolsLister: ippoolInformer.Lister(),
ippoolsSynced: ippoolInformer.Informer().HasSynced,
poolsLister: vxnetpoolInformer.Lister(),
Expand Down
Loading

0 comments on commit c11a1be

Please sign in to comment.