Skip to content

Commit

Permalink
fix: Use domain names in etcd parameters and modify the logic for cre…
Browse files Browse the repository at this point in the history
…ating SVCs

Signed-off-by: baoyinghai_yewu <[email protected]>
  • Loading branch information
OrangeBao committed Oct 31, 2024
1 parent eda0695 commit f11a334
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,7 @@ spec:
- --listen-peer-urls=http://[::]:{{ .EtcdListenPeerPort }}
- --advertise-client-urls=https://{{ .EtcdClientService }}.{{ .Namespace }}.svc.cluster.local:{{ .EtcdListenClientPort }}
- --initial-cluster={{ .InitialCluster }}
{{ if .IPV6First }}
- --initial-advertise-peer-urls=http://[$(PODIP)]:2380
{{ else }}
- --initial-advertise-peer-urls=http://$(PODIP):2380
{{ end }}
- --initial-advertise-peer-urls=http://$(VIRTUAL_ETCD_NAME).{{ .EtcdPeerServiceName }}.{{ .Namespace }}.svc.cluster.local:2380
- --initial-cluster-state=new
- --client-cert-auth=true
- --trusted-ca-file=/etc/virtualcluster/pki/etcd/etcd-ca.crt
Expand Down
26 changes: 17 additions & 9 deletions pkg/kubenest/util/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,35 @@ import (
v1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
kerrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
utilyaml "k8s.io/apimachinery/pkg/util/yaml"
"k8s.io/client-go/dynamic"
clientset "k8s.io/client-go/kubernetes"
"k8s.io/client-go/util/retry"
"k8s.io/klog/v2"
"sigs.k8s.io/yaml"
)

func CreateOrUpdateService(client clientset.Interface, svc *v1.Service) error {
_, err := client.CoreV1().Services(svc.GetNamespace()).Update(context.TODO(), svc, metav1.UpdateOptions{})
_, err := client.CoreV1().Services(svc.GetNamespace()).Create(context.TODO(), svc, metav1.CreateOptions{})
if err != nil {
if !apierrors.IsNotFound(err) {
return err
}
if err := retry.RetryOnConflict(retry.DefaultRetry, func() error {
currentSvc, err := client.CoreV1().Services(svc.GetNamespace()).Get(context.TODO(), svc.GetName(), metav1.GetOptions{})
if err != nil {
return err
}

_, err := client.CoreV1().Services(svc.GetNamespace()).Create(context.TODO(), svc, metav1.CreateOptions{})
if err != nil {
svc.ResourceVersion = currentSvc.ResourceVersion

_, err = client.CoreV1().Services(svc.GetNamespace()).Update(context.TODO(), svc, metav1.UpdateOptions{})
if err != nil {
return err
}
return nil
}); err != nil {
return err
}
}
Expand Down Expand Up @@ -297,7 +305,7 @@ func ApplyObject(dynamicClient dynamic.Interface, obj *unstructured.Unstructured
// Get the existing resource
existingObj, err := resourceClient.Get(context.TODO(), name, metav1.GetOptions{})
if err != nil {
if kerrors.IsNotFound(err) {
if apierrors.IsNotFound(err) {
// If not found, create the resource
_, err = resourceClient.Create(context.TODO(), obj, metav1.CreateOptions{})
if err != nil {
Expand Down Expand Up @@ -451,7 +459,7 @@ func ReplaceObject(dynamicClient dynamic.Interface, obj *unstructured.Unstructur
// Get the existing resource
_, err := resourceClient.Get(context.TODO(), name, metav1.GetOptions{})
if err != nil {
if kerrors.IsNotFound(err) {
if apierrors.IsNotFound(err) {
// If not found, create the resource
_, err = resourceClient.Create(context.TODO(), obj, metav1.CreateOptions{})
if err != nil {
Expand Down

0 comments on commit f11a334

Please sign in to comment.