Skip to content

Commit

Permalink
resolve conflict and update konnect client
Browse files Browse the repository at this point in the history
  • Loading branch information
randmonkey committed Feb 7, 2023
1 parent 6fda5bb commit 4b02244
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 43 deletions.
10 changes: 10 additions & 0 deletions internal/adminapi/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,13 @@ func (c *Client) KonnectRuntimeGroup() string {

return c.konnectRuntimeGroup
}

// SetLastConfigSHA overrides last config SHA.
func (c *Client) SetLastConfigSHA(s []byte) {
c.lastConfigSHA = s
}

// LastConfigSHA returns a checksum of the last successful configuration push.
func (c *Client) LastConfigSHA() []byte {
return c.lastConfigSHA
}
2 changes: 1 addition & 1 deletion internal/adminapi/konnect.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type KonnectConfig struct {
TLSClient TLSClientConfig
}

func NewKongClientForKonnectRuntimeGroup(ctx context.Context, c KonnectConfig) (*Client, error) {
func NewKongClientForKonnectRuntimeGroup(ctx context.Context, c KonnectConfig) (Client, error) {
tlsClientCert, err := tlsutil.ValueFromVariableOrFile([]byte(c.TLSClient.Cert), c.TLSClient.CertFile)
if err != nil {
return Client{}, fmt.Errorf("could not extract TLS client cert: %w", err)
Expand Down
5 changes: 2 additions & 3 deletions internal/konnect/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var (
// operating runtime group with ID in AdminClient.
KicAPIPathPattern = "%s/kic/api/runtime_groups/%s"
// KicNodeAPIPathPattern is the path pattern for KIC node operations.
KicNodeAPIPathPattern = "%s/kic/api/runtime_groups/%s/kic-nodes"
KicNodeAPIPathPattern = "%s/kic/api/runtime_groups/%s/v1/kic-nodes"
)

// NewAdminClient creates a Konnect client.
Expand All @@ -41,8 +41,7 @@ func NewAdminClient(cfg adminapi.KonnectConfig) (*AdminClient, error) {
}

tlsConfig := tls.Config{ //nolint:gosec
InsecureSkipVerify: true,
Certificates: []tls.Certificate{},
Certificates: []tls.Certificate{},
}
if len(tlsClientCert) > 0 && len(tlsClientKey) > 0 {
// Read the key pair to create certificate
Expand Down
39 changes: 29 additions & 10 deletions internal/konnect/node_agent.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package konnect

import (
"fmt"
"time"

"github.com/go-logr/logr"

"github.com/kong/kubernetes-ingress-controller/v2/internal/util"
)

var (
defaultRefreshNodeInterval = 15 * time.Second
)
var defaultRefreshNodeInterval = 15 * time.Second

type NodeAgent struct {
NodeID string
Expand All @@ -24,7 +23,6 @@ type NodeAgent struct {
}

func NewNodeAgent(hostname string, version string, logger logr.Logger, adminClient *AdminClient) *NodeAgent {

return &NodeAgent{
Hostname: hostname,
Version: version,
Expand All @@ -37,7 +35,6 @@ func NewNodeAgent(hostname string, version string, logger logr.Logger, adminClie
}

func (a *NodeAgent) createNode() error {

createNodeReq := &CreateNodeRequest{
ID: a.NodeID,
Hostname: a.Hostname,
Expand All @@ -56,21 +53,43 @@ func (a *NodeAgent) createNode() error {
return nil
}

func (a *NodeAgent) updateNode() error {
func (a *NodeAgent) clearOutdatedNodes() error {
nodes, err := a.adminClient.ListNodes()
if err != nil {
return fmt.Errorf("failed to list nodes: %w", err)
}

ingressControllerStatus := &IngressControllerStatus{
State: IngressControllerStateOperational,
for _, node := range nodes.Items {
if node.Type == NodeTypeIngressController && node.Hostname != a.Hostname {
a.Logger.V(util.DebugLevel).Info("remove KIC node", "node_id", node.ID, "hostname", node.Hostname)
err := a.adminClient.DeleteNode(node.ID)
if err != nil {
return fmt.Errorf("failed to delete node %s: %w", node.ID, err)
}
}
}
return nil
}

func (a *NodeAgent) updateNode() error {
err := a.clearOutdatedNodes()
if err != nil {
a.Logger.Error(err, "failed to clear outdated nodes")
return err
}

// TODO: retrieve the real state of KIC
ingressControllerStatus := IngressControllerStateOperational

updateNodeReq := &UpdateNodeRequest{
Hostname: a.Hostname,
Type: NodeTypeIngressController,
Version: a.Version,
LastPing: time.Now().Unix(),

IngressControllerStatus: ingressControllerStatus,
Status: string(ingressControllerStatus),
}
_, err := a.adminClient.UpdateNode(a.NodeID, updateNodeReq)
_, err = a.adminClient.UpdateNode(a.NodeID, updateNodeReq)
if err != nil {
a.Logger.Error(err, "failed to update node for KIC")
return err
Expand Down
55 changes: 26 additions & 29 deletions internal/konnect/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,21 @@ package konnect

const (
NodeTypeIngressController = "ingress-controller"
NodeTypeKongProxy = "kong-proxy"
NodeTypeIngressProxy = "ingress-proxy"
)

type NodeItem struct {
ID string `json:"id"`
Version string `json:"version"`
Hostname string `json:"hostname"`
LastPing int64 `json:"last_ping"`
Type string `json:"type"`
CreatedAt int64 `json:"created_at"`
UpdatedAt int64 `json:"updated_at"`
ConfigHash string `json:"config_hash"`
CompatibilityStatus *CompatibilityStatus `json:"compatibility_status,omitempty"`
IngressControllerStatus *IngressControllerStatus `json:"ingress_controller_status,omitempty"`
ID string `json:"id"`
Version string `json:"version"`
Hostname string `json:"hostname"`
LastPing int64 `json:"last_ping"`
Type string `json:"type"`
CreatedAt int64 `json:"created_at"`
UpdatedAt int64 `json:"updated_at"`
ConfigHash string `json:"config_hash"`
CompatibilityStatus *CompatibilityStatus `json:"compatibility_status,omitempty"`
Status string `json:"status,omitempty"`
}

type CompatibilityState string
Expand Down Expand Up @@ -55,33 +56,29 @@ const (
IngressControllerStateUnknown IngressControllerState = "INGRESS_CONTROLLER_STATE_UNKNOWN"
)

type IngressControllerStatus struct {
State IngressControllerState `json:"state"`
}

type CreateNodeRequest struct {
ID string `json:"id,omitempty"`
Hostname string `json:"hostname"`
Type string `json:"type"`
LastPing int64 `json:"last_ping"`
Version string `json:"version"`
CompatabilityStatus *CompatibilityStatus `json:"compatibility_status,omitempty"`
IngressControllerStatus *IngressControllerStatus `json:"ingress_controller_status,omitempty"`
ConfigHash string `json:"config_hash,omitempty"`
ID string `json:"id,omitempty"`
Hostname string `json:"hostname"`
Type string `json:"type"`
LastPing int64 `json:"last_ping"`
Version string `json:"version"`
CompatabilityStatus *CompatibilityStatus `json:"compatibility_status,omitempty"`
Status string `json:"status,omitempty"`
ConfigHash string `json:"config_hash,omitempty"`
}

type CreateNodeResponse struct {
Item *NodeItem `json:"item"`
}

type UpdateNodeRequest struct {
Hostname string `json:"hostname"`
Type string `json:"type"`
LastPing int64 `json:"last_ping"`
Version string `json:"version"`
ConfigHash string `json:"config_hash,omitempty"`
CompatabilityStatus *CompatibilityStatus `json:"compatibility_status,omitempty"`
IngressControllerStatus *IngressControllerStatus `json:"ingress_controller_status,omitempty"`
Hostname string `json:"hostname"`
Type string `json:"type"`
LastPing int64 `json:"last_ping"`
Version string `json:"version"`
ConfigHash string `json:"config_hash,omitempty"`
CompatabilityStatus *CompatibilityStatus `json:"compatibility_status,omitempty"`
Status string `json:"status,omitempty"`
}

type UpdateNodeResponse struct {
Expand Down

0 comments on commit 4b02244

Please sign in to comment.