-
Notifications
You must be signed in to change notification settings - Fork 907
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Mohamed Awnallah <mohamedmohey2352@gmail.com>
- Loading branch information
1 parent
8cc712f
commit cd7022f
Showing
2 changed files
with
272 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,208 @@ | ||
/* | ||
Copyright 2024 The Karmada 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 tasks | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
clientset "k8s.io/client-go/kubernetes" | ||
"k8s.io/client-go/rest" | ||
|
||
operatorv1alpha1 "github.com/karmada-io/karmada/operator/pkg/apis/operator/v1alpha1" | ||
"github.com/karmada-io/karmada/operator/pkg/certs" | ||
) | ||
|
||
// TestInterface defines the interface for retrieving test data. | ||
type TestInterface interface { | ||
// Get returns the data from the test instance. | ||
Get() string | ||
} | ||
|
||
// MyTestData is a struct that implements the TestInterface. | ||
type MyTestData struct { | ||
Data string | ||
} | ||
|
||
// Get returns the data stored in the MyTestData struct. | ||
func (m *MyTestData) Get() string { | ||
return m.Data | ||
} | ||
|
||
// TestInitData contains the configuration and state required to initialize Karmada components. | ||
type TestInitData struct { | ||
name string | ||
namespace string | ||
controlplaneConfig *rest.Config | ||
dataDir string | ||
crdTarball operatorv1alpha1.CRDTarball | ||
karmadaVersion string | ||
components *operatorv1alpha1.KarmadaComponents | ||
featureGates map[string]bool | ||
remoteClient clientset.Interface | ||
karmadaClient clientset.Interface | ||
controlplaneAddr string | ||
certs []*certs.KarmadaCert | ||
} | ||
|
||
// Ensure TestInitData implements InitData interface at compile time. | ||
var _ InitData = &TestInitData{} | ||
|
||
// GetName returns the name of the current Karmada installation. | ||
func (t *TestInitData) GetName() string { | ||
return t.name | ||
} | ||
|
||
// GetNamespace returns the namespace of the current Karmada installation. | ||
func (t *TestInitData) GetNamespace() string { | ||
return t.namespace | ||
} | ||
|
||
// SetControlplaneConfig sets the control plane configuration for Karmada. | ||
func (t *TestInitData) SetControlplaneConfig(config *rest.Config) { | ||
t.controlplaneConfig = config | ||
} | ||
|
||
// ControlplaneConfig returns the control plane configuration. | ||
func (t *TestInitData) ControlplaneConfig() *rest.Config { | ||
return t.controlplaneConfig | ||
} | ||
|
||
// ControlplaneAddress returns the address of the control plane. | ||
func (t *TestInitData) ControlplaneAddress() string { | ||
return t.controlplaneAddr | ||
} | ||
|
||
// RemoteClient returns the Kubernetes client for remote interactions. | ||
func (t *TestInitData) RemoteClient() clientset.Interface { | ||
return t.remoteClient | ||
} | ||
|
||
// KarmadaClient returns the Kubernetes client for interacting with Karmada. | ||
func (t *TestInitData) KarmadaClient() clientset.Interface { | ||
return t.karmadaClient | ||
} | ||
|
||
// DataDir returns the data directory used by Karmada. | ||
func (t *TestInitData) DataDir() string { | ||
return t.dataDir | ||
} | ||
|
||
// CrdTarball returns the CRD tarball used for Karmada installation. | ||
func (t *TestInitData) CrdTarball() operatorv1alpha1.CRDTarball { | ||
return t.crdTarball | ||
} | ||
|
||
// KarmadaVersion returns the version of Karmada being used. | ||
func (t *TestInitData) KarmadaVersion() string { | ||
return t.karmadaVersion | ||
} | ||
|
||
// Components returns the Karmada components used in the current installation. | ||
func (t *TestInitData) Components() *operatorv1alpha1.KarmadaComponents { | ||
return t.components | ||
} | ||
|
||
// FeatureGates returns the feature gates enabled for the current installation. | ||
func (t *TestInitData) FeatureGates() map[string]bool { | ||
return t.featureGates | ||
} | ||
|
||
// AddCert adds a Karmada certificate to the TestInitData. | ||
func (t *TestInitData) AddCert(cert *certs.KarmadaCert) { | ||
t.certs = append(t.certs, cert) | ||
} | ||
|
||
// GetCert retrieves a Karmada certificate by its name. | ||
func (t *TestInitData) GetCert(name string) *certs.KarmadaCert { | ||
for _, cert := range t.certs { | ||
if cert.CertName() == name { | ||
return cert | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
// CertList returns a list of all Karmada certificates stored in TestInitData. | ||
func (t *TestInitData) CertList() []*certs.KarmadaCert { | ||
return t.certs | ||
} | ||
|
||
// LoadCertFromSecret loads a Karmada certificate from a Kubernetes secret. | ||
func (t *TestInitData) LoadCertFromSecret(secret *corev1.Secret) error { | ||
if len(secret.Data) == 0 { | ||
return fmt.Errorf("cert data is empty") | ||
} | ||
|
||
// Dummy implementation: load empty certificate. | ||
cert := &certs.KarmadaCert{} | ||
t.AddCert(cert) | ||
return nil | ||
} | ||
|
||
// createPods creates a specified number of pods in the given namespace | ||
// with the provided component name. | ||
// | ||
// Parameters: | ||
// - client: Kubernetes client interface. | ||
// - namespace: Namespace for pod creation. | ||
// - componentName: Base name for pods and containers. | ||
// - replicaCount: Number of pods to create. | ||
// - labels: Labels for pods creation. | ||
// | ||
// Returns an error if pod creation fails. | ||
func createPods(client clientset.Interface, namespace string, componentName string, replicaCount int32, labels map[string]string) error { | ||
for i := int32(0); i < replicaCount; i++ { | ||
podName := fmt.Sprintf("%s-pod-%d", componentName, i) | ||
pod := &corev1.Pod{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: podName, | ||
Namespace: namespace, | ||
Labels: labels, | ||
}, | ||
Spec: corev1.PodSpec{ | ||
Containers: []corev1.Container{{ | ||
Name: fmt.Sprintf("my-%s-container-%d", componentName, i), | ||
Image: fmt.Sprintf("my-%s-image:latest", componentName), | ||
Ports: []corev1.ContainerPort{{ContainerPort: 80}}, | ||
}}, | ||
}, | ||
} | ||
_, err := client.CoreV1().Pods(namespace).Create(context.TODO(), pod, metav1.CreateOptions{}) | ||
if err != nil { | ||
return fmt.Errorf("failed to create pod %s: %w", podName, err) | ||
} | ||
|
||
// Mark the pod as in running state. | ||
pod.Status = corev1.PodStatus{ | ||
Phase: corev1.PodRunning, | ||
Conditions: []corev1.PodCondition{ | ||
{ | ||
Type: corev1.PodReady, | ||
Status: corev1.ConditionTrue, | ||
}, | ||
}, | ||
} | ||
_, err = client.CoreV1().Pods(namespace).UpdateStatus(context.TODO(), pod, metav1.UpdateOptions{}) | ||
if err != nil { | ||
return fmt.Errorf("failed to create update status of the pod %s: %w", podName, err) | ||
} | ||
} | ||
return nil | ||
} |
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