mirror of
https://github.com/zc2638/drone-k8s-plugin.git
synced 2026-06-16 14:48:59 +08:00
Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 4f0c2ee15b | |||
| ec0a62a0fa | |||
| a675739e91 |
@@ -30,7 +30,15 @@ jobs:
|
||||
|
||||
- name: Set up Docker Buildx
|
||||
id: buildx
|
||||
uses: docker/setup-buildx-action@master
|
||||
uses: docker/setup-buildx-action@v2
|
||||
|
||||
- name: Cache Docker layers
|
||||
uses: actions/cache@v3
|
||||
with:
|
||||
path: /tmp/.buildx-cache
|
||||
key: ${{ runner.os }}-buildx-${{ github.sha }}
|
||||
restore-keys: |
|
||||
${{ runner.os }}-buildx-
|
||||
|
||||
- name: Login to Docker Hub
|
||||
uses: docker/login-action@v2
|
||||
@@ -39,15 +47,20 @@ jobs:
|
||||
password: ${{ secrets.DOCKER_SECRET }}
|
||||
|
||||
- name: Build and publish ${{ matrix.target.Dockerfile }}
|
||||
uses: docker/build-push-action@v2
|
||||
uses: docker/build-push-action@v3
|
||||
with:
|
||||
context: .
|
||||
push: true
|
||||
builder: ${{ steps.buildx.outputs.name }}
|
||||
file: ${{ matrix.target.Dockerfile }}
|
||||
platforms: linux/amd64,linux/arm64,linux/arm
|
||||
cache-from: type=gha,scope=${{ github.workflow }}
|
||||
cache-to: type=gha,mode=max,scope=${{ github.workflow }}
|
||||
cache-from: type=local,src=/tmp/.buildx-cache
|
||||
cache-to: type=local,dest=/tmp/.buildx-cache-new,mode=max
|
||||
tags: |
|
||||
zc2638/drone-k8s-plugin:${{ steps.prepare.outputs.full_tag_name }}
|
||||
zc2638/drone-k8s-plugin:${{ steps.prepare.outputs.latest_tag }}
|
||||
|
||||
- name: Move cache
|
||||
run: |
|
||||
rm -rf /tmp/.buildx-cache
|
||||
mv /tmp/.buildx-cache-new /tmp/.buildx-cache
|
||||
|
||||
+43
-4
@@ -23,6 +23,8 @@ import (
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
pkgruntime "k8s.io/apimachinery/pkg/runtime"
|
||||
|
||||
v1 "k8s.io/api/core/v1"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
@@ -52,11 +54,13 @@ func run(cfg *Config, kubeClient kubernetes.Interface, dynamicClient dynamic.Int
|
||||
matches := pluginExp.FindStringSubmatch(v)
|
||||
key := strings.ToLower(matches[1])
|
||||
envMap[key] = matches[2]
|
||||
logrus.Debugf("env: %s=%s", key, matches[2])
|
||||
}
|
||||
if droneExp.MatchString(v) {
|
||||
matches := droneExp.FindStringSubmatch(v)
|
||||
key := strings.ToLower(matches[1])
|
||||
envMap[key] = matches[2]
|
||||
logrus.Debugf("env: %s=%s", key, matches[2])
|
||||
}
|
||||
}
|
||||
|
||||
@@ -75,15 +79,15 @@ func run(cfg *Config, kubeClient kubernetes.Interface, dynamicClient dynamic.Int
|
||||
}
|
||||
mapping := restmapper.NewDiscoveryRESTMapper(gr)
|
||||
|
||||
logrus.Debug("start to apply resources from init templates")
|
||||
logrus.Debug("Start to apply resources from init templates")
|
||||
if err := applyResources(dynamicClient, mapping, initObjSet, cfg.Namespace); err != nil {
|
||||
return err
|
||||
}
|
||||
logrus.Debug("start to apply configmaps from config files")
|
||||
logrus.Debug("Start to apply configmaps from config files")
|
||||
if err := applyForConfig(kubeClient, cfg.GetConfigFiles()); err != nil {
|
||||
return err
|
||||
}
|
||||
logrus.Debug("start to apply resources from templates")
|
||||
logrus.Debug("Start to apply resources from templates")
|
||||
if err := applyResources(dynamicClient, mapping, objSet, cfg.Namespace); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -140,7 +144,7 @@ func applyResources(
|
||||
WithField("kind", gvk.Kind).
|
||||
WithField("namespace", objCopy.GetNamespace()).
|
||||
WithField("name", objCopy.GetName()).
|
||||
Info("Apply resource")
|
||||
Info("Apply Resource")
|
||||
|
||||
restMapping, err := mapping.RESTMapping(gvk.GroupKind(), gvk.Version)
|
||||
if err != nil {
|
||||
@@ -172,6 +176,15 @@ func applyResources(
|
||||
},
|
||||
})
|
||||
if err == nil {
|
||||
switch objCopy.GetKind() {
|
||||
case "Service":
|
||||
objCopy, err = completeService(origin, objCopy)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
default:
|
||||
}
|
||||
|
||||
rv, _ := strconv.ParseInt(origin.GetResourceVersion(), 10, 64)
|
||||
objCopy.SetResourceVersion(strconv.FormatInt(rv, 10))
|
||||
if _, err = resourceInter.Update(ctx, objCopy, metav1.UpdateOptions{}); err != nil {
|
||||
@@ -194,6 +207,32 @@ func applyResources(
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func completeService(origin, obj *unstructured.Unstructured) (*unstructured.Unstructured, error) {
|
||||
var (
|
||||
originSvc v1.Service
|
||||
objSvc v1.Service
|
||||
)
|
||||
if err := pkgruntime.DefaultUnstructuredConverter.FromUnstructured(origin.UnstructuredContent(), &originSvc); err != nil {
|
||||
return nil, fmt.Errorf("convert origin unstructured object %s to Service failed: %v", obj.GetName(), err)
|
||||
}
|
||||
if err := pkgruntime.DefaultUnstructuredConverter.FromUnstructured(obj.UnstructuredContent(), &objSvc); err != nil {
|
||||
return nil, fmt.Errorf("convert unstructured object %s to Service failed: %v", obj.GetName(), err)
|
||||
}
|
||||
|
||||
objSvc.Spec.ClusterIP = originSvc.Spec.ClusterIP
|
||||
objSvc.Spec.ClusterIPs = originSvc.Spec.ClusterIPs
|
||||
|
||||
unstructuredContent, err := pkgruntime.DefaultUnstructuredConverter.ToUnstructured(&objSvc)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("convert Service %s to unstructured object failed: %v", objSvc.GetName(), err)
|
||||
}
|
||||
|
||||
current := &unstructured.Unstructured{}
|
||||
current.SetUnstructuredContent(unstructuredContent)
|
||||
return current, nil
|
||||
}
|
||||
|
||||
func applyForConfig(kubeClient kubernetes.Interface, cfs []ConfigFile) error {
|
||||
if len(cfs) == 0 {
|
||||
return nil
|
||||
|
||||
Vendored
+4
-4
@@ -1,14 +1,14 @@
|
||||
apiVersion: v1
|
||||
kind: Service
|
||||
metadata:
|
||||
name: {{ app_name }}
|
||||
namespace: {{ app_namespace }}
|
||||
name: {{ .env.app_name }}
|
||||
namespace: {{ .env.app_namespace }}
|
||||
labels:
|
||||
app: {{ app_name }}
|
||||
app: {{ .env.app_name }}
|
||||
spec:
|
||||
ports:
|
||||
- nodePort: 30090
|
||||
port: 9090
|
||||
selector:
|
||||
app: {{ app_name }}
|
||||
app: {{ .env.app_name }}
|
||||
type: NodePort
|
||||
|
||||
Reference in New Issue
Block a user