-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: ResourceConsist Controller (#24)
* feat: ResourceConsist Controller
- Loading branch information
1 parent
19eef7c
commit cc066e5
Showing
24 changed files
with
2,638 additions
and
13 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
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,32 @@ | ||
/* | ||
Copyright 2023 The KusionStack Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package controllers | ||
|
||
import ( | ||
"sigs.k8s.io/controller-runtime/pkg/manager" | ||
|
||
"kusionstack.io/kafed/pkg/controllers/alibaba_cloud_slb" | ||
"kusionstack.io/kafed/pkg/controllers/resourceconsist" | ||
) | ||
|
||
func init() { | ||
AddToManagerFuncs = append(AddToManagerFuncs, Add) | ||
} | ||
|
||
func Add(manager manager.Manager) error { | ||
return resourceconsist.Add(manager, alibaba_cloud_slb.NewReconcileAdapter(manager.GetClient())) | ||
} |
94 changes: 94 additions & 0 deletions
94
pkg/controllers/alibaba_cloud_slb/alibaba_cloud_slb_client.go
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,94 @@ | ||
/* | ||
Copyright 2023 The KusionStack Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package alibaba_cloud_slb | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client" | ||
slb20140515 "github.com/alibabacloud-go/slb-20140515/v4/client" | ||
util "github.com/alibabacloud-go/tea-utils/v2/service" | ||
"github.com/alibabacloud-go/tea/tea" | ||
) | ||
|
||
var ( | ||
slbAccessKeyID string | ||
slbAccessKeySecret string | ||
slbEndpoint string | ||
|
||
alibabaCloudSlbClient *AlibabaCloudSlbClient | ||
) | ||
|
||
type AlibabaCloudSlbClient struct { | ||
*slb20140515.Client | ||
} | ||
|
||
func NewAlibabaCloudSlbClient() (*AlibabaCloudSlbClient, error) { | ||
if slbAccessKeyID == "" || slbAccessKeySecret == "" { | ||
return nil, fmt.Errorf("slbAccessKeyID or slbAccessKeySecret is empty") | ||
} | ||
config := &openapi.Config{ | ||
AccessKeyId: tea.String(slbAccessKeyID), | ||
AccessKeySecret: tea.String(slbAccessKeySecret), | ||
// Endpoint, refer: https://api.aliyun.com/product/Slb | ||
Endpoint: tea.String(slbEndpoint), | ||
} | ||
|
||
slbClient, err := slb20140515.NewClient(config) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &AlibabaCloudSlbClient{ | ||
slbClient, | ||
}, nil | ||
} | ||
|
||
func (c *AlibabaCloudSlbClient) GetBackendServers(lbID string) ([]string, error) { | ||
describeHealthStatusRequest := &slb20140515.DescribeHealthStatusRequest{ | ||
LoadBalancerId: tea.String(lbID), | ||
} | ||
runtime := &util.RuntimeOptions{} | ||
|
||
resp, err := c.DescribeHealthStatusWithOptions(describeHealthStatusRequest, runtime) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if resp == nil || resp.Body == nil || resp.Body.BackendServers == nil { | ||
return nil, fmt.Errorf("get backend servers list faield, resp is nil") | ||
} | ||
|
||
backendServers := make([]string, len(resp.Body.BackendServers.BackendServer)) | ||
for idx, bs := range resp.Body.BackendServers.BackendServer { | ||
backendServers[idx] = *bs.ServerIp | ||
} | ||
return backendServers, nil | ||
} | ||
|
||
func init() { | ||
if os.Getenv("ALIYUN_SLB_AK_KEY") != "" { | ||
slbAccessKeyID = os.Getenv("ALIYUN_SLB_AK_KEY") | ||
} | ||
if os.Getenv("ALIYUN_SLB_AK_SECRET") != "" { | ||
slbAccessKeySecret = os.Getenv("ALIYUN_SLB_AK_SECRET") | ||
} | ||
if os.Getenv("ALIYUN_SLB_ENDPOINT") != "" { | ||
slbEndpoint = os.Getenv("ALIYUN_SLB_ENDPOINT") | ||
} | ||
|
||
alibabaCloudSlbClient, _ = NewAlibabaCloudSlbClient() | ||
} |
191 changes: 191 additions & 0 deletions
191
pkg/controllers/alibaba_cloud_slb/alibaba_cloud_slb_controller.go
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,191 @@ | ||
/* | ||
Copyright 2023 The KusionStack Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package alibaba_cloud_slb | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
corev1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/labels" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
|
||
"kusionstack.io/kafed/pkg/controllers/resourceconsist" | ||
) | ||
|
||
var _ resourceconsist.ReconcileAdapter = &ReconcileAdapter{} | ||
|
||
type ReconcileAdapter struct { | ||
client.Client | ||
} | ||
|
||
func NewReconcileAdapter(c client.Client) *ReconcileAdapter { | ||
return &ReconcileAdapter{ | ||
Client: c, | ||
} | ||
} | ||
|
||
func (r *ReconcileAdapter) GetControllerName() string { | ||
return "alibaba-cloud-slb-controller" | ||
} | ||
|
||
func (r *ReconcileAdapter) NotFollowPodOpsLifeCycle() bool { | ||
return false | ||
} | ||
|
||
func (r *ReconcileAdapter) GetExpectEmployer(ctx context.Context, employer client.Object) ([]resourceconsist.IEmployer, error) { | ||
return nil, nil | ||
} | ||
|
||
func (r *ReconcileAdapter) GetSelectedEmployeeNames(ctx context.Context, employer client.Object) ([]string, error) { | ||
svc, ok := employer.(*corev1.Service) | ||
if !ok { | ||
return nil, fmt.Errorf("expect employer kind is Service") | ||
} | ||
selector := labels.Set(svc.Spec.Selector).AsSelectorPreValidated() | ||
var podList corev1.PodList | ||
err := r.List(ctx, &podList, &client.ListOptions{Namespace: svc.Namespace, LabelSelector: selector}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
selected := make([]string, len(podList.Items)) | ||
for idx, pod := range podList.Items { | ||
selected[idx] = pod.Name | ||
} | ||
|
||
return selected, nil | ||
} | ||
|
||
func (r *ReconcileAdapter) GetCurrentEmployer(ctx context.Context, employer client.Object) ([]resourceconsist.IEmployer, error) { | ||
return nil, nil | ||
} | ||
|
||
func (r *ReconcileAdapter) CreateEmployer(employer client.Object, toCreate []resourceconsist.IEmployer) ([]resourceconsist.IEmployer, []resourceconsist.IEmployer, error) { | ||
return nil, nil, nil | ||
} | ||
|
||
func (r *ReconcileAdapter) UpdateEmployer(employer client.Object, toUpdate []resourceconsist.IEmployer) ([]resourceconsist.IEmployer, []resourceconsist.IEmployer, error) { | ||
return nil, nil, nil | ||
} | ||
|
||
func (r *ReconcileAdapter) DeleteEmployer(employer client.Object, toDelete []resourceconsist.IEmployer) ([]resourceconsist.IEmployer, []resourceconsist.IEmployer, error) { | ||
return nil, nil, nil | ||
} | ||
|
||
func (r *ReconcileAdapter) RecordEmployer(succCreate, succUpdate, succDelete []resourceconsist.IEmployer) error { | ||
return nil | ||
} | ||
|
||
func (r *ReconcileAdapter) GetExpectEmployee(ctx context.Context, employer client.Object) ([]resourceconsist.IEmployee, error) { | ||
svc, ok := employer.(*corev1.Service) | ||
if !ok { | ||
return nil, fmt.Errorf("expect employer kind is Service") | ||
} | ||
selector := labels.Set(svc.Spec.Selector).AsSelectorPreValidated() | ||
var podList corev1.PodList | ||
err := r.List(ctx, &podList, &client.ListOptions{Namespace: svc.Namespace, LabelSelector: selector}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
expected := make([]resourceconsist.IEmployee, len(podList.Items)) | ||
for idx, pod := range podList.Items { | ||
status := AlibabaSlbPodStatus{ | ||
EmployeeID: pod.Status.PodIP, | ||
EmployeeName: pod.Name, | ||
} | ||
employeeStatuses, err := resourceconsist.GetCommonPodEmployeeStatus(&pod) | ||
if err != nil { | ||
return nil, err | ||
} | ||
extraStatus := PodExtraStatus{} | ||
if employeeStatuses.LifecycleReady { | ||
extraStatus.TrafficOn = true | ||
} else { | ||
extraStatus.TrafficOn = false | ||
} | ||
employeeStatuses.ExtraStatus = extraStatus | ||
status.EmployeeStatuses = employeeStatuses | ||
expected[idx] = status | ||
} | ||
|
||
return expected, nil | ||
} | ||
|
||
func (r *ReconcileAdapter) GetCurrentEmployee(ctx context.Context, employer client.Object) ([]resourceconsist.IEmployee, error) { | ||
svc, ok := employer.(*corev1.Service) | ||
if !ok { | ||
return nil, fmt.Errorf("expect employer kind is Service") | ||
} | ||
selector := labels.Set(svc.Spec.Selector).AsSelectorPreValidated() | ||
var podList corev1.PodList | ||
err := r.List(ctx, &podList, &client.ListOptions{Namespace: svc.Namespace, LabelSelector: selector}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
lbID := svc.GetLabels()[alibabaCloudSlbLbIdLabelKey] | ||
bsExistUnderSlb := make(map[string]bool) | ||
if lbID != "" { | ||
backendServers, err := alibabaCloudSlbClient.GetBackendServers(lbID) | ||
if err != nil { | ||
return nil, fmt.Errorf("get backend servers of slb failed, err: %s", err.Error()) | ||
} | ||
for _, bs := range backendServers { | ||
bsExistUnderSlb[bs] = true | ||
} | ||
} | ||
|
||
current := make([]resourceconsist.IEmployee, len(podList.Items)) | ||
for idx, pod := range podList.Items { | ||
status := AlibabaSlbPodStatus{ | ||
EmployeeID: pod.Status.PodIP, | ||
EmployeeName: pod.Name, | ||
} | ||
employeeStatuses, err := resourceconsist.GetCommonPodEmployeeStatus(&pod) | ||
if err != nil { | ||
return nil, err | ||
} | ||
extraStatus := PodExtraStatus{} | ||
if !bsExistUnderSlb[status.EmployeeID] { | ||
extraStatus.TrafficOn = false | ||
} else { | ||
extraStatus.TrafficOn = true | ||
} | ||
employeeStatuses.ExtraStatus = extraStatus | ||
status.EmployeeStatuses = employeeStatuses | ||
current[idx] = status | ||
} | ||
|
||
return current, nil | ||
} | ||
|
||
// CreateEmployees returns (nil, toCreate, nil) since CCM of ACK will sync bs of slb | ||
func (r *ReconcileAdapter) CreateEmployees(employer client.Object, toCreate []resourceconsist.IEmployee) ([]resourceconsist.IEmployee, []resourceconsist.IEmployee, error) { | ||
return nil, toCreate, nil | ||
} | ||
|
||
// UpdateEmployees returns (nil, toUpdate, nil) since CCM of ACK will sync bs of slb | ||
func (r *ReconcileAdapter) UpdateEmployees(employer client.Object, toUpdate []resourceconsist.IEmployee) ([]resourceconsist.IEmployee, []resourceconsist.IEmployee, error) { | ||
return nil, toUpdate, nil | ||
} | ||
|
||
// DeleteEmployees returns (nil, toDelete, nil) since CCM of ACK will sync bs of slb | ||
func (r *ReconcileAdapter) DeleteEmployees(employer client.Object, toDelete []resourceconsist.IEmployee) ([]resourceconsist.IEmployee, []resourceconsist.IEmployee, error) { | ||
return nil, toDelete, nil | ||
} |
Oops, something went wrong.