Skip to content

Commit

Permalink
feat: feat/direct pod ip (#76)
Browse files Browse the repository at this point in the history
* chore: guard against missing secrets

Signed-off-by: Alex Jones <alexsimonjones@gmail.com>

* fix: use direct pod IP to bypass DNS caching issues

Signed-off-by: Alex Jones <alexsimonjones@gmail.com>

* chore: solving the issue with reconciliation

Signed-off-by: Alex Jones <alexsimonjones@gmail.com>

---------

Signed-off-by: Alex Jones <alexsimonjones@gmail.com>
  • Loading branch information
AlexsJones authored May 10, 2023
1 parent c3059bc commit 5d82413
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 19 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
__debug_bin
*.DS_Store
k8sgpt-operator
# Binaries for programs and plugins
Expand Down
44 changes: 40 additions & 4 deletions controllers/k8sgpt_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,18 @@ package controllers
import (
"context"
"fmt"
"net"
"os"
"strings"
"time"

corev1alpha1 "github.com/k8sgpt-ai/k8sgpt-operator/api/v1alpha1"

kclient "github.com/k8sgpt-ai/k8sgpt-operator/pkg/client"
"github.com/k8sgpt-ai/k8sgpt-operator/pkg/resources"
"github.com/k8sgpt-ai/k8sgpt-operator/pkg/utils"
"github.com/prometheus/client_golang/prometheus"
v1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
Expand Down Expand Up @@ -144,9 +145,36 @@ func (r *K8sGPTReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr
if os.Getenv("LOCAL_MODE") != "" {
address = "localhost:8080"
} else {
address = fmt.Sprintf("%s.%s:8080", "k8sgpt", deployment.Namespace)
// Get k8sgpt-deployment service pod ip
podList := &corev1.PodList{}
listOpts := []client.ListOption{
client.InNamespace(k8sgptConfig.Namespace),
client.MatchingLabels{"app": "k8sgpt-deployment"},
}
err := r.List(ctx, podList, listOpts...)
if err != nil {
k8sgptReconcileErrorCount.Inc()
return r.finishReconcile(err, false)
}
if len(podList.Items) == 0 {
k8sgptReconcileErrorCount.Inc()
return r.finishReconcile(fmt.Errorf("no pods found for k8sgpt-deployment"), false)
}
address = fmt.Sprintf("%s:8080", podList.Items[0].Status.PodIP)
}

fmt.Printf("Creating new client for %s\n", address)
// Test if the port is open
conn, err := net.DialTimeout("tcp", address, 1*time.Second)
if err != nil {
k8sgptReconcileErrorCount.Inc()
return r.finishReconcile(err, false)
}

fmt.Printf("Connection established between %s and localhost with time out of %d seconds.\n", address, int64(1))
fmt.Printf("Remote Address : %s \n", conn.RemoteAddr().String())
fmt.Printf("Local Address : %s \n", conn.LocalAddr().String())

k8sgptClient, err := kclient.NewClient(address)
if err != nil {
k8sgptReconcileErrorCount.Inc()
Expand Down Expand Up @@ -184,8 +212,16 @@ func (r *K8sGPTReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr
// if the result already exists, we will update it
if errors.IsAlreadyExists(err) {

result.ResourceVersion = k8sgptConfig.GetResourceVersion()
err = r.Update(ctx, &result)
// Get the actual result with metadata rather than our local construct
var newResult corev1alpha1.Result
err = r.Get(ctx, client.ObjectKey{Namespace: k8sgptConfig.Namespace,
Name: name}, &newResult)
if err != nil {
k8sgptReconcileErrorCount.Inc()
return r.finishReconcile(err, false)
}
newResult.Spec = resultSpec
err = r.Update(ctx, &newResult)
if err != nil {
k8sgptReconcileErrorCount.Inc()
return r.finishReconcile(err, false)
Expand Down
43 changes: 28 additions & 15 deletions pkg/resources/k8sgpt.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
)

Expand Down Expand Up @@ -253,37 +254,37 @@ func Sync(ctx context.Context, c client.Client,

var objs []client.Object

svc, err := GetService(config)
if err != nil {
return err
svc, er := GetService(config)
if er != nil {
return er
}

objs = append(objs, svc)

svcAcc, err := GetServiceAccount(config)
if err != nil {
return err
svcAcc, er := GetServiceAccount(config)
if er != nil {
return er
}

objs = append(objs, svcAcc)

clusterRole, err := GetClusterRole(config)
if err != nil {
return err
clusterRole, er := GetClusterRole(config)
if er != nil {
return er
}

objs = append(objs, clusterRole)

clusterRoleBinding, err := GetClusterRoleBinding(config)
if err != nil {
return err
clusterRoleBinding, er := GetClusterRoleBinding(config)
if er != nil {
return er
}

objs = append(objs, clusterRoleBinding)

deployment, err := GetDeployment(config)
if err != nil {
return err
deployment, er := GetDeployment(config)
if er != nil {
return er
}

objs = append(objs, deployment)
Expand All @@ -292,6 +293,18 @@ func Sync(ctx context.Context, c client.Client,
for _, obj := range objs {
switch i {
case Create:

// before creation we will check to see if the secret exists if used as a ref
if config.Spec.Secret != nil {

secret := &v1.Secret{}
er := c.Get(ctx, types.NamespacedName{Name: config.Spec.Secret.Name,
Namespace: config.Namespace}, secret)
if er != nil {
return err.New("references secret does not exist, cannot create deployment")
}
}

err := c.Create(ctx, obj)
if err != nil {
// If the object already exists, ignore the error
Expand Down

0 comments on commit 5d82413

Please sign in to comment.