-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extended status check for reconciliation
- Loading branch information
Showing
2 changed files
with
111 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package factory | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/aenix-io/etcd-operator/api/v1alpha1" | ||
clientv3 "go.etcd.io/etcd/client/v3" | ||
v1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/types" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
func NewEtcdClientSet(ctx context.Context, cluster *v1alpha1.EtcdCluster, client client.Client) (*clientv3.Client, []*clientv3.Client, error) { | ||
cfg, err := configFromCluster(ctx, cluster, client) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
if len(cfg.Endpoints) == 0 { | ||
return nil, nil, nil | ||
} | ||
eps := cfg.Endpoints | ||
clusterClient, err := clientv3.New(cfg) | ||
if err != nil { | ||
return nil, nil, fmt.Errorf("error building etcd cluster client: %w", err) | ||
} | ||
singleClients := make([]*clientv3.Client, len(eps)) | ||
for i, ep := range eps { | ||
cfg.Endpoints = []string{ep} | ||
singleClients[i], err = clientv3.New(cfg) | ||
if err != nil { | ||
return nil, nil, fmt.Errorf("error building etcd single-endpoint client for endpoint %s: %w", ep, err) | ||
} | ||
} | ||
return clusterClient, singleClients, nil | ||
} | ||
|
||
func configFromCluster(ctx context.Context, cluster *v1alpha1.EtcdCluster, client client.Client) (clientv3.Config, error) { | ||
ep := v1.Endpoints{} | ||
err := client.Get(ctx, types.NamespacedName{Name: GetHeadlessServiceName(cluster), Namespace: cluster.Namespace}, &ep) | ||
if err != nil { | ||
return clientv3.Config{}, err | ||
} | ||
names := map[string]struct{}{} | ||
urls := make([]string, 0, 8) | ||
for _, v := range ep.Subsets { | ||
for _, addr := range v.Addresses { | ||
names[addr.Hostname] = struct{}{} | ||
} | ||
for _, addr := range v.NotReadyAddresses { | ||
names[addr.Hostname] = struct{}{} | ||
} | ||
} | ||
for name := range names { | ||
urls = append(urls, fmt.Sprintf("%s:%s", name, "2379")) | ||
} | ||
|
||
return clientv3.Config{Endpoints: urls}, nil | ||
} |