Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update gateway status #1138

Merged
merged 8 commits into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/04.Cloud-Native/4.2.Gateway-API.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ rules:
- apiGroups: ["gateway.networking.k8s.io"]
resources: ["gatewayclasses", "httproutes", "gateways"]
verbs: ["get", "watch", "list"]
- apiGroups: ["gateway.networking.k8s.io"]
resources: ["gatewayclasses/status", "gateways/status"]
verbs: ["update"]

---
apiVersion: v1
Expand Down
1 change: 1 addition & 0 deletions pkg/object/gatewaycontroller/gatewaycontroller.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ func (gc *GatewayController) run() {
}
logger.Infof("successfully watched gateway related resources")

gc.translate()
// process resource update events
for {
select {
Expand Down
28 changes: 7 additions & 21 deletions pkg/object/gatewaycontroller/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ func (c *k8sClient) watch(namespaces []string) (chan struct{}, error) {
}

c.gwFactory = gwinformers.NewSharedInformerFactoryWithOptions(c.gwcs, resyncPeriod, gwinformers.WithTweakListOptions(notHelm))
_, err = c.gwFactory.Gateway().V1beta1().GatewayClasses().Informer().AddEventHandler(c)
_, err = c.gwFactory.Gateway().V1().GatewayClasses().Informer().AddEventHandler(c)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -347,34 +347,17 @@ func (c *k8sClient) GetGatewayClasses(controllerName string) []*gwapis.GatewayCl
return result
}

func (c *k8sClient) UpdateGatewayClassStatus(gatewayClass *gwapis.GatewayClass, condition metav1.Condition) error {
func (c *k8sClient) UpdateGatewayClassStatus(gatewayClass *gwapis.GatewayClass, status gwapis.GatewayClassStatus) error {
gc := gatewayClass.DeepCopy()

var newConditions []metav1.Condition
for _, cond := range gc.Status.Conditions {
// No update for identical condition.
if cond.Type == condition.Type && cond.Status == condition.Status {
return nil
}

// Keep other condition types.
if cond.Type != condition.Type {
newConditions = append(newConditions, cond)
}
}

// Append the condition to update.
newConditions = append(newConditions, condition)
gc.Status.Conditions = newConditions
gc.Status = status

ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

_, err := c.gwcs.GatewayV1().GatewayClasses().UpdateStatus(ctx, gc, metav1.UpdateOptions{})
if err != nil {
return fmt.Errorf("failed to update GatewayClass %q status: %w", gatewayClass.Name, err)
return fmt.Errorf("failed to update GatewayClass %s/%s status: %w", gatewayClass.Namespace, gatewayClass.Name, err)
}

return nil
}

Expand Down Expand Up @@ -475,6 +458,9 @@ func (c *k8sClient) isNamespaceWatched(ns string) bool {
return true
}
for _, watchedNamespace := range c.namespaces {
if watchedNamespace == metav1.NamespaceAll {
return true
}
if watchedNamespace == ns {
return true
}
Expand Down
99 changes: 99 additions & 0 deletions pkg/object/gatewaycontroller/translator.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
"github.com/megaease/easegress/v2/pkg/util/pathadaptor"
"golang.org/x/exp/slices"
apicorev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
gwapis "sigs.k8s.io/gateway-api/apis/v1"
)

Expand Down Expand Up @@ -616,13 +617,111 @@ func (st *specTranslator) translate() error {
st.routes = st.k8sClient.GetHTTPRoutes()

for _, c := range classes {
err := st.updateGatewayClassStatus(c)
if err != nil {
logger.Errorf("%v", err)
}
for _, g := range gateways {
if string(g.Spec.GatewayClassName) != c.Name {
continue
}
err := st.updateGatewayStatus(c, g)
if err != nil {
logger.Errorf("%v", err)
}
st.translateGateway(c, g)
}
}

return nil
}

// Conditions:
//
// Last Transition Time: 1970-01-01T00:00:00Z
// Message: Waiting for controller
// Reason: Pending
// Status: Unknown
// Type: Accepted
// Last Transition Time: 1970-01-01T00:00:00Z
// Message: Waiting for controller
// Reason: Pending
// Status: Unknown
// Type: Programmed
//
// we need to remove init conditions and add new.
func (st *specTranslator) updateGatewayStatus(c *gwapis.GatewayClass, g *gwapis.Gateway) error {
compareCondition := func(c1, c2 metav1.Condition) bool {
return c1.Type == c2.Type && c1.Status == c2.Status && c1.Reason == c2.Reason && c1.Message == c2.Message
}
condition := metav1.Condition{
Type: string(gwapis.GatewayConditionAccepted),
Status: metav1.ConditionTrue,
Reason: string(gwapis.GatewayReasonAccepted),
Message: "Gateway is accepted",
LastTransitionTime: metav1.Now(),
}

gateway := g.DeepCopy()
newConditions := []metav1.Condition{}
for _, cond := range gateway.Status.Conditions {
// remove the init condition.
if cond.Type == string(gwapis.GatewayConditionAccepted) && cond.Status == metav1.ConditionUnknown && cond.Message == "Waiting for controller" {
continue
}
if cond.Type == string(gwapis.GatewayConditionProgrammed) && cond.Status == metav1.ConditionUnknown && cond.Message == "Waiting for controller" {
continue
}
// if we already have the condition, just return.
if compareCondition(cond, condition) {
return nil
}
newConditions = append(newConditions, cond)
}
newConditions = append(newConditions, condition)
gateway.Status.Conditions = newConditions
return st.k8sClient.UpdateGatewayStatus(g, gateway.Status)
}

// GatewayClass starts with status:
// Status:
//
// Conditions:
// Last Transition Time: 1970-01-01T00:00:00Z
// Message: Waiting for controller
// Reason: Waiting
// Status: Unknown
// Type: Accepted
//
// we need to remove init status and add new.
func (st *specTranslator) updateGatewayClassStatus(c *gwapis.GatewayClass) error {
compareCondition := func(c1, c2 metav1.Condition) bool {
return c1.Type == c2.Type && c1.Status == c2.Status && c1.Reason == c2.Reason && c1.Message == c2.Message
}
condition := metav1.Condition{
Type: string(gwapis.GatewayClassConditionStatusAccepted),
Status: metav1.ConditionTrue,
Reason: string(gwapis.GatewayClassReasonAccepted),
Message: "GatewayClass is accepted",
LastTransitionTime: metav1.Now(),
}

gc := c.DeepCopy()
newConditions := []metav1.Condition{}
for _, cond := range gc.Status.Conditions {
// remove the init condition.
if cond.Type == string(gwapis.GatewayClassConditionStatusAccepted) && cond.Status == metav1.ConditionUnknown && cond.Message == "Waiting for controller" {
continue
}
// if we already have the condition, just return.
if compareCondition(cond, condition) {
return nil
}
newConditions = append(newConditions, cond)
}

newConditions = append(newConditions, condition)
gc.Status.Conditions = newConditions
err := st.k8sClient.UpdateGatewayClassStatus(c, gc.Status)
return err
}
4 changes: 3 additions & 1 deletion pkg/object/httpserver/routers/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,9 @@ func (rule *Rule) Init() {
if !h.IsRegexp {
if h.Value != "" {
count := strings.Count(h.Value, "*")
if count > 1 {
if count == 0 {
continue
} else if count > 1 {
logger.Errorf("invalid host %s, only one wildcard is allowed", h.Value)
continue
}
Expand Down
Loading