Skip to content

Commit

Permalink
feat(main): add apis node (#57)
Browse files Browse the repository at this point in the history
Signed-off-by: cuisongliu <cuisongliu@qq.com>
  • Loading branch information
cuisongliu authored Oct 19, 2024
1 parent 11e5b41 commit d832db1
Show file tree
Hide file tree
Showing 7 changed files with 379 additions and 92 deletions.
18 changes: 14 additions & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
uses: codecov/codecov-action@v4
with:
token: ${{ secrets.CODECOV_TOKEN }}
job2:
job1:
runs-on: ubuntu-20.04
steps:
- name: Checkout
Expand All @@ -48,9 +48,16 @@ jobs:
cd deploy
sudo sealos build -t ghcr.io/${{ github.repository_owner }}/automq-operator-sealos:latest .
job1:
job2:
runs-on: ubuntu-20.04
steps:
- name: Before freeing up disk space
run: |
echo "Before freeing up disk space"
echo "=============================================================================="
df -hT
echo "=============================================================================="
- name: Checkout
uses: actions/checkout@master

Expand All @@ -62,7 +69,7 @@ jobs:
- name: Verify sealos
run: |
curl -sfL https://raw.githubusercontent.com/labring/sealos/v5.0.0/scripts/install.sh | sh -s v5.0.0 labring/sealos
- name: prune os
- name: install k8s and apps
run: |
sudo systemctl unmask containerd
sudo systemctl unmask docker
Expand All @@ -74,7 +81,10 @@ jobs:
sudo rm -rf /run/containerd/containerd.sock
sudo sealos run labring/kubernetes:v1.27.7
sudo sealos run labring/helm:v3.9.4 labring/calico:v3.26.5 labring/openebs:v3.9.0 labring/cert-manager:v1.14.6
sudo sealos run labring/minio:RELEASE.2024-01-11T07-46-16Z labring/kube-prometheus-stack:v0.63.0 labring/kafka-ui:v0.7.1
sudo sealos run labring/minio:RELEASE.2024-01-11T07-46-16Z labring/kube-prometheus-stack:v0.63.0
sudo sealos run labring/kafka-ui:v0.7.1
sleep 10
sudo kubectl get pods -A --show-labels
- name: build
run: |
sudo make e2e
Expand Down
29 changes: 1 addition & 28 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,13 @@ package main

import (
"flag"
v1 "k8s.io/api/core/v1"
"os"
"sigs.k8s.io/controller-runtime/pkg/client"

// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
// to ensure that exec-entrypoint and run can make use of them.
_ "k8s.io/client-go/plugin/pkg/client/auth"

infrav1beta1 "github.com/cuisongliu/automq-operator/api/v1beta1"
"github.com/cuisongliu/automq-operator/internal/controller"
"github.com/gin-gonic/gin"
promv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
Expand Down Expand Up @@ -130,30 +126,7 @@ func main() {

go func() {
if mgr.GetCache().WaitForCacheSync(ctx) {
setupLog.Info("cache sync success")
router := gin.Default()
router.GET("/api/v1/nodes/:name", func(c *gin.Context) {
name := c.Param("name")
node := &v1.Node{}
node.Name = name
if noe := mgr.GetClient().Get(ctx, client.ObjectKeyFromObject(node), node); noe != nil {
c.JSON(500, gin.H{"message": noe.Error()})
return
}
nodeIP := ""
for _, addr := range node.Status.Addresses {
if addr.Type == v1.NodeInternalIP {
nodeIP = addr.Address
break
}
}
if nodeIP == "" {
c.JSON(500, gin.H{"message": "node ip not found"})
return
}
c.String(200, nodeIP)
})
router.Run(":9090")
controller.APIRegistry(ctx, mgr.GetClient())
}
}()

Expand Down
96 changes: 96 additions & 0 deletions e2e/automq_cluster_controller_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
Copyright 2024 cuisongliu@qq.com.
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 e2e

import (
"context"
"os"
"time"

infrav1beta1 "github.com/cuisongliu/automq-operator/api/v1beta1"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
)

var _ = Describe("automq_controller", func() {
Context("automq_controller cr tests", func() {
ctx := context.Background()
namespaceName := "automq-operator"
namespace := &v1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: namespaceName,
Namespace: namespaceName,
},
}
automq := &infrav1beta1.AutoMQ{}
automq.Name = "automq-s1"
automq.Namespace = namespaceName
automq.Spec.ClusterID = "rZdE0DjZSrqy96PXrMUZVw"

BeforeEach(func() {
By("Creating the Namespace to perform the tests")
err := k8sClient.Create(ctx, namespace)
Expect(err).To(Not(HaveOccurred()))
By("Setting the NAMESPACE_NAME ENV VAR which stores the Operand image")
err = os.Setenv("NAMESPACE_NAME", namespaceName)
Expect(err).To(Not(HaveOccurred()))
})
It("Update Endpoint", func() {
By("creating the custom resource for the automq")
err := k8sClient.Get(ctx, client.ObjectKeyFromObject(automq), automq)
if err != nil && errors.IsNotFound(err) {
// Let's mock our custom resource at the same way that we would
// apply on the cluster the manifest under config/samples
automq.Spec.S3.Endpoint = "http://minio.minio.svc.cluster.local:9000"
automq.Spec.S3.Bucket = "ko3"
automq.Spec.S3.AccessKeyID = "admin"
automq.Spec.S3.SecretAccessKey = "minio123"
automq.Spec.S3.Region = "us-east-1"
automq.Spec.S3.EnablePathStyle = true
automq.Spec.Controller.Replicas = 1
automq.Spec.Broker.Replicas = 3
automq.Spec.NodePort = 32009
err = k8sClient.Create(ctx, automq)
Expect(err).To(Not(HaveOccurred()))
}
})
AfterEach(func() {
By("removing the custom resource for the automq")
found := &infrav1beta1.AutoMQ{}
err := k8sClient.Get(ctx, client.ObjectKeyFromObject(automq), found)
Expect(err).To(Not(HaveOccurred()))

Eventually(func() error {
return k8sClient.Delete(context.TODO(), found)
}, 2*time.Minute, time.Second).Should(Succeed())

// TODO(user): Attention if you improve this code by adding other context test you MUST
// be aware of the current delete namespace limitations.
// More info: https://book.kubebuilder.io/reference/envtest.html#testing-considerations
By("Deleting the Namespace to perform the tests")
_ = k8sClient.Delete(ctx, namespace)

By("Removing the Image ENV VAR which stores the Operand image")
_ = os.Unsetenv("NAMESPACE_NAME")
})
})

})
Loading

0 comments on commit d832db1

Please sign in to comment.