Skip to content

Commit

Permalink
fix(renewtls): fix renewtls typo
Browse files Browse the repository at this point in the history
fix renewtls typo

Signed-off-by: ysicing <[email protected]>
  • Loading branch information
ysicing committed Nov 9, 2022
1 parent a9b5242 commit 5e83812
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 13 deletions.
4 changes: 2 additions & 2 deletions backend/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -734,7 +734,7 @@ const docTemplate = `{
"settings": {
"type": "array",
"items": {
"$ref": "#/definitions/model.stringSetting"
"$ref": "#/definitions/model.StringSetting"
}
},
"settings_map": {
Expand Down Expand Up @@ -831,7 +831,7 @@ const docTemplate = `{
}
}
},
"model.stringSetting": {
"model.StringSetting": {
"type": "object",
"properties": {
"key": {
Expand Down
4 changes: 2 additions & 2 deletions backend/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -725,7 +725,7 @@
"settings": {
"type": "array",
"items": {
"$ref": "#/definitions/model.stringSetting"
"$ref": "#/definitions/model.StringSetting"
}
},
"settings_map": {
Expand Down Expand Up @@ -822,7 +822,7 @@
}
}
},
"model.stringSetting": {
"model.StringSetting": {
"type": "object",
"properties": {
"key": {
Expand Down
4 changes: 2 additions & 2 deletions backend/docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ definitions:
type: string
settings:
items:
$ref: '#/definitions/model.stringSetting'
$ref: '#/definitions/model.StringSetting'
type: array
settings_map:
additionalProperties: true
Expand Down Expand Up @@ -82,7 +82,7 @@ definitions:
required:
- name
type: object
model.stringSetting:
model.StringSetting:
properties:
key:
type: string
Expand Down
4 changes: 4 additions & 0 deletions backend/internal/app/patch/patch.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// Copyright (c) 2022 北京渠成软件有限公司 All rights reserved.
// Use of this source code is governed by Z PUBLIC LICENSE 1.2 (ZPL 1.2)
// license that can be found in the LICENSE file.

package patch

import "context"
Expand Down
5 changes: 5 additions & 0 deletions backend/internal/app/patch/patch20221103.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// Copyright (c) 2022 北京渠成软件有限公司 All rights reserved.
// Use of this source code is governed by Z PUBLIC LICENSE 1.2 (ZPL 1.2)
// license that can be found in the LICENSE file.

package patch

import (
Expand All @@ -8,6 +12,7 @@ import (
"github.com/Masterminds/semver"
"github.com/sirupsen/logrus"
"github.com/spf13/viper"

"gitlab.zcorp.cc/pangu/cne-api/internal/app/model"
"gitlab.zcorp.cc/pangu/cne-api/internal/app/service"
"gitlab.zcorp.cc/pangu/cne-api/internal/pkg/constant"
Expand Down
3 changes: 2 additions & 1 deletion backend/internal/app/serve/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ package serve
import (
"context"
"fmt"
"gitlab.zcorp.cc/pangu/cne-api/internal/app/patch"
"net/http"
"os"
"time"

"gitlab.zcorp.cc/pangu/cne-api/internal/app/patch"

"github.com/spf13/viper"

"gitlab.zcorp.cc/pangu/cne-api/internal/pkg/analysis"
Expand Down
7 changes: 6 additions & 1 deletion backend/internal/app/service/app/instance/domain.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
// Copyright (c) 2022 北京渠成软件有限公司 All rights reserved.
// Use of this source code is governed by Z PUBLIC LICENSE 1.2 (ZPL 1.2)
// license that can be found in the LICENSE file.

package instance

import (
"strings"

"gitlab.zcorp.cc/pangu/cne-api/internal/app/model"
"gitlab.zcorp.cc/pangu/cne-api/internal/pkg/constant"
"strings"
)

func (i *Instance) ListIngressHosts() []string {
Expand Down
4 changes: 4 additions & 0 deletions backend/internal/pkg/constant/env.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// Copyright (c) 2022 北京渠成软件有限公司 All rights reserved.
// Use of this source code is governed by Z PUBLIC LICENSE 1.2 (ZPL 1.2)
// license that can be found in the LICENSE file.

package constant

const (
Expand Down
26 changes: 21 additions & 5 deletions backend/internal/pkg/job/httptls.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ import (
// CheckReNewCertificate 检查证书是否过期
func CheckReNewCertificate() error {
logger := logging.DefaultLogger().WithField("job", "renewtls")
domain := os.Getenv("APP_DOMAIN")
kubeCluster := cluster.Get("primary")
// 兼容老版本域名, 域名从ingress获取
domain := domainGet(kubeCluster, logger)

if strings.HasSuffix(domain, "haogs.cn") || strings.HasSuffix(domain, "corp.cc") {
needRenew, err := checkCertificate(domain, logger)
Expand All @@ -38,7 +40,7 @@ func CheckReNewCertificate() error {
return err
}
if needRenew {
return renewCertificate(domain, logger)
return renewCertificate(kubeCluster, domain, logger)
}
logger.Infof("skip domain %s renew tls", domain)
return nil
Expand All @@ -47,6 +49,19 @@ func CheckReNewCertificate() error {
return nil
}

func domainGet(kubeCluster *cluster.Cluster, logger logrus.FieldLogger) string {
defaultDomain := os.Getenv("APP_DOMAIN")
ingressClient := kubeCluster.Clients.Base.NetworkingV1().Ingresses(viper.GetString(constant.FlagRuntimeNamespace))
ingress, err := ingressClient.Get(context.TODO(), "qucheng", metav1.GetOptions{})
if err != nil {
return defaultDomain
}
if ingress != nil && len(ingress.Spec.Rules) > 0 {
return ingress.Spec.Rules[0].Host
}
return defaultDomain
}

func checkCertificate(domain string, logger logrus.FieldLogger) (bool, error) {
client := &http.Client{
Transport: &http.Transport{
Expand Down Expand Up @@ -74,11 +89,12 @@ func checkCertificate(domain string, logger logrus.FieldLogger) (bool, error) {
return false, nil
}

func renewCertificate(domain string, logger logrus.FieldLogger) error {
func renewCertificate(kubeCluster *cluster.Cluster, domain string, logger logrus.FieldLogger) error {
logging.DefaultLogger().WithField("job", "renewtls").Info("try create renewtls job")
ds := strings.Split(domain, ".")
coreDomain := fmt.Sprintf("%s.%s.%s", ds[len(ds)-3], ds[len(ds)-2], ds[len(ds)-1])
// renew default tls certificate
// renew ingress tls certificate
kubeCluster := cluster.Get("primary")
jobclient := kubeCluster.Clients.Base.BatchV1().Jobs(viper.GetString(constant.FlagRuntimeNamespace))
object := &batchv1.Job{
TypeMeta: metav1.TypeMeta{
Expand All @@ -103,7 +119,7 @@ func renewCertificate(domain string, logger logrus.FieldLogger) error {
Env: []corev1.EnvVar{
{
Name: "DOMAIN",
Value: domain,
Value: coreDomain,
},
},
},
Expand Down

0 comments on commit 5e83812

Please sign in to comment.