Skip to content

Commit

Permalink
feat: support networkingv1 and crd v1 with discovery
Browse files Browse the repository at this point in the history
Signed-off-by: iutx <root@viper.run>
  • Loading branch information
iutx committed Sep 15, 2022
1 parent 80ac5b1 commit 20f4cf4
Show file tree
Hide file tree
Showing 12 changed files with 667 additions and 223 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,5 @@ require (
k8s.io/apiextensions-apiserver v0.21.2
k8s.io/apimachinery v0.21.2
k8s.io/client-go v12.0.0+incompatible
k8s.io/utils v0.0.0-20210527160623-6fdb442a123b // indirect
)
40 changes: 40 additions & 0 deletions pkg/cluster/ingress/helper/common/annotations.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright (c) 2021 Terminus, Inc.
//
// This program is free software: you can use, redistribute, and/or modify
// it under the terms of the GNU Affero General Public License, version 3
// or later ("AGPL"), as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

package common

import (
"os"

"github.com/erda-project/dice-operator/pkg/cluster/ingress/helper/types"
)

func Annotations(svcName string) map[string]string {
enableAccessLog := "false"

if os.Getenv(types.EnableComponentAccessLog) == "true" {
enableAccessLog = "true"
}

annotation := map[string]string{
"nginx.ingress.kubernetes.io/enable-access-log": enableAccessLog,
}

switch svcName {
case "gittar", "erda-server", "ui":
annotation["nginx.ingress.kubernetes.io/proxy-body-size"] = "0"
default:
}

return annotation
}
45 changes: 45 additions & 0 deletions pkg/cluster/ingress/helper/common/convert.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright (c) 2021 Terminus, Inc.
//
// This program is free software: you can use, redistribute, and/or modify
// it under the terms of the GNU Affero General Public License, version 3
// or later ("AGPL"), as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

package common

import (
"github.com/erda-project/dice-operator/pkg/spec"
"github.com/erda-project/dice-operator/pkg/cluster/ingress/helper/types"
"fmt"
"github.com/erda-project/erda/pkg/strutil"
)

func ConvertHost(svcName string, cluster *spec.DiceCluster) []string {
customDomain := cluster.Spec.CustomDomain

// Rename, logic will in tools render future.
// erda-server.* will convert to openapi.*
if svcName == types.ErdaServer {
svcName = types.Openapi
}

if domains, ok := customDomain[svcName]; ok {
return strutil.Map(strutil.Split(domains, ",", true), func(s string) string { return strutil.Trim(s) })
}
r, ok := map[string][]string{
"ui": {
fmt.Sprintf("dice.%s", cluster.Spec.PlatformDomain),
fmt.Sprintf("*.%s", cluster.Spec.PlatformDomain),
},
}[svcName]
if !ok {
return []string{fmt.Sprintf("%s.%s", svcName, cluster.Spec.PlatformDomain)}
}
return r
}
164 changes: 164 additions & 0 deletions pkg/cluster/ingress/helper/extension/v1beta1/ingress.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
// Copyright (c) 2021 Terminus, Inc.
//
// This program is free software: you can use, redistribute, and/or modify
// it under the terms of the GNU Affero General Public License, version 3
// or later ("AGPL"), as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

package v1

import (
"fmt"
"context"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
extensions "k8s.io/api/extensions/v1beta1"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/client-go/kubernetes/typed/extensions/v1beta1"

"github.com/erda-project/dice-operator/pkg/cluster/ingress/helper/types"
"github.com/erda-project/dice-operator/pkg/cluster/ingress/helper/common"
"github.com/erda-project/erda/pkg/parser/diceyml"
"github.com/erda-project/dice-operator/pkg/spec"
)

type Ingress struct {
c v1beta1.ExtensionsV1beta1Interface
}

func NewIngress(c v1beta1.ExtensionsV1beta1Interface) *Ingress {
return &Ingress{c: c}
}

func (i *Ingress) CreateIfNotExists(svcName string, svc *diceyml.Service, cluster *spec.DiceCluster, ownerRefs []metav1.OwnerReference) error {
generatedIngress, err := buildIngress(svcName, svc, cluster, ownerRefs)
if err != nil {
return err
}

if _, err = i.c.Ingresses(cluster.Namespace).Get(context.Background(), generatedIngress.Name,
metav1.GetOptions{}); err != nil && !errors.IsNotFound(err) {
return nil
}

if _, err = i.c.Ingresses(cluster.Namespace).Create(context.Background(), generatedIngress,
metav1.CreateOptions{}); err != nil {
return err
}

return err
}

func (i *Ingress) CreateOrUpdate(svcName string, svc *diceyml.Service, cluster *spec.DiceCluster, ownerRefs []metav1.OwnerReference) error {
generatedIngress, err := buildIngress(svcName, svc, cluster, ownerRefs)
if err != nil {
return err
}

if _, err = i.c.Ingresses(cluster.Namespace).Get(context.Background(), generatedIngress.Name,
metav1.GetOptions{}); err != nil {
if !errors.IsNotFound(err) {
return err
}

if _, err := i.c.Ingresses(cluster.Namespace).Create(context.Background(), generatedIngress,
metav1.CreateOptions{}); err != nil {
return err
}

return nil
}

if _, err := i.c.Ingresses(cluster.Namespace).Update(context.Background(), generatedIngress,
metav1.UpdateOptions{}); errors.IsForbidden(err) || errors.IsInvalid(err) {
_ = i.c.Ingresses(cluster.Namespace).Delete(context.Background(), generatedIngress.Name, metav1.DeleteOptions{})
_, err := i.c.Ingresses(cluster.Namespace).Create(context.Background(), generatedIngress, metav1.CreateOptions{})
if err != nil {
return err
}
}

return nil
}

func (i *Ingress) Delete(svcName string, cluster *spec.DiceCluster) error {
err := i.c.Ingresses(cluster.Namespace).Delete(context.Background(), svcName, metav1.DeleteOptions{})
if errors.IsNotFound(err) {
return nil
}

return err
}

func buildIngress(
svcName string,
svc *diceyml.Service,
cluster *spec.DiceCluster,
ownerRefs []metav1.OwnerReference) (*extensions.Ingress, error) {
if len(svc.Expose) == 0 {
return nil, fmt.Errorf("svc: %v, not exposed any port, check diceyml", svc)
}
rules := make([]extensions.IngressRule, 0)
for _, host := range common.ConvertHost(svcName, cluster) {
rules = append(rules, extensions.IngressRule{
Host: host,
IngressRuleValue: extensions.IngressRuleValue{
HTTP: &extensions.HTTPIngressRuleValue{
Paths: GenHTTPIngressPaths(svcName, svc.Expose[0]),
},
},
})
}

tls := []extensions.IngressTLS{
{
Hosts: common.ConvertHost(svcName, cluster),
},
}

return &extensions.Ingress{
ObjectMeta: metav1.ObjectMeta{
Name: svcName,
Namespace: cluster.Namespace,
OwnerReferences: ownerRefs,
Annotations: common.Annotations(svcName),
},
Spec: extensions.IngressSpec{
Rules: rules,
TLS: tls,
},
}, nil
}

func GenHTTPIngressPaths(svcName string, exposePort int) []extensions.HTTPIngressPath {

var httpIngressPaths []extensions.HTTPIngressPath

httpIngressPath := extensions.HTTPIngressPath{
Backend: extensions.IngressBackend{
ServiceName: svcName,
ServicePort: intstr.FromInt(exposePort),
},
}

httpIngressPaths = append(httpIngressPaths, httpIngressPath)

if svcName == types.ErdaServer {
// TODO: remove register interface
httpIngressPaths = append(httpIngressPaths, extensions.HTTPIngressPath{
Backend: extensions.IngressBackend{
ServiceName: types.ClusterManager,
ServicePort: intstr.FromInt(80),
},
Path: types.AgentRegisterPath,
})
}
return httpIngressPaths
}
58 changes: 58 additions & 0 deletions pkg/cluster/ingress/helper/ingress.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright (c) 2021 Terminus, Inc.
//
// This program is free software: you can use, redistribute, and/or modify
// it under the terms of the GNU Affero General Public License, version 3
// or later ("AGPL"), as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

package helper

import (
"github.com/erda-project/erda/pkg/parser/diceyml"
"github.com/erda-project/dice-operator/pkg/spec"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"github.com/erda-project/dice-operator/pkg/cluster/ingress/helper/networking/v1"
"github.com/sirupsen/logrus"
networkingv1 "k8s.io/api/networking/v1"
"github.com/erda-project/dice-operator/pkg/utils"
extensionsv1beta1 "k8s.io/api/extensions/v1beta1"
v1beta1 "github.com/erda-project/dice-operator/pkg/cluster/ingress/helper/extension/v1beta1"
)

type IngressHelper interface {
CreateIfNotExists(
svcName string,
svc *diceyml.Service,
cluster *spec.DiceCluster,
ownerRefs []metav1.OwnerReference,
) error

CreateOrUpdate(
svcName string,
svc *diceyml.Service,
cluster *spec.DiceCluster,
ownerRefs []metav1.OwnerReference,
) error

Delete(
svcName string,
cluster *spec.DiceCluster,
) error
}

func New(c kubernetes.Interface) IngressHelper {
if utils.VersionHas(extensionsv1beta1.SchemeGroupVersion.String()) {
logrus.Infof("ingress helper use version: %s", extensionsv1beta1.SchemeGroupVersion.String())
return v1beta1.NewIngress(c.ExtensionsV1beta1())
}

logrus.Infof("ingress helper use version: %s", networkingv1.SchemeGroupVersion.String())
return v1.NewIngress(c.NetworkingV1())
}
Loading

0 comments on commit 20f4cf4

Please sign in to comment.