Skip to content
Open
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: 1 addition & 2 deletions api/v1alpha1/sandbox_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,7 @@ type Lifecycle struct {
// SandboxStatus defines the observed state of Sandbox.
type SandboxStatus struct {
// serviceFQDN that is valid for default cluster settings
// Limitation: Hardcoded to the domain .cluster.local
// e.g. sandbox-example.default.svc.cluster.local
// The domain defaults to cluster.local but is configurable via the controller's --cluster-domain flag.
// +optional
ServiceFQDN string `json:"serviceFQDN,omitempty"`

Expand Down
3 changes: 3 additions & 0 deletions cmd/agent-sandbox-controller/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ func main() {
var leaderElectionNamespace string
var probeAddr string
var extensions bool
var clusterDomain string
var enableTracing bool
var enablePprof bool
var enablePprofDebug bool
Expand All @@ -61,6 +62,7 @@ func main() {
var sandboxClaimConcurrentWorkers int
var sandboxWarmPoolConcurrentWorkers int
var sandboxTemplateConcurrentWorkers int
flag.StringVar(&clusterDomain, "cluster-domain", "cluster.local", "Kubernetes cluster domain for service FQDN generation")
flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.")
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
flag.BoolVar(&enableLeaderElection, "leader-elect", true,
Expand Down Expand Up @@ -209,6 +211,7 @@ func main() {
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
Tracer: instrumenter,
ClusterDomain: clusterDomain,
}).SetupWithManager(mgr, sandboxConcurrentWorkers); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "Sandbox")
os.Exit(1)
Expand Down
10 changes: 5 additions & 5 deletions controllers/sandbox_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ type SandboxReconciler struct {
client.Client
Scheme *runtime.Scheme
Tracer asmetrics.Instrumenter
ClusterDomain string
}

//+kubebuilder:rbac:groups=agents.x-k8s.io,resources=sandboxes,verbs=get;list;watch;create;update;patch;delete
Expand Down Expand Up @@ -290,7 +291,7 @@ func (r *SandboxReconciler) reconcileService(ctx context.Context, sandbox *sandb
}
} else {
log.Info("Found Service", "Service.Namespace", service.Namespace, "Service.Name", service.Name)
setServiceStatus(sandbox, service)
r.setServiceStatus(sandbox, service)
return service, nil
}

Expand Down Expand Up @@ -322,15 +323,14 @@ func (r *SandboxReconciler) reconcileService(ctx context.Context, sandbox *sandb
return nil, err
}

setServiceStatus(sandbox, service)
r.setServiceStatus(sandbox, service)
return service, nil
}

// setServiceStatus updates the sandbox status with the service name and FQDN.
// TODO(barney-s): hardcoded to svc.cluster.local which is the default. Need a way to change it.
func setServiceStatus(sandbox *sandboxv1alpha1.Sandbox, service *corev1.Service) {
func (r *SandboxReconciler) setServiceStatus(sandbox *sandboxv1alpha1.Sandbox, service *corev1.Service) {
sandbox.Status.Service = service.Name
sandbox.Status.ServiceFQDN = service.Name + "." + service.Namespace + ".svc.cluster.local"
sandbox.Status.ServiceFQDN = service.Name + "." + service.Namespace + ".svc." + r.ClusterDomain
}

func (r *SandboxReconciler) reconcilePod(ctx context.Context, sandbox *sandboxv1alpha1.Sandbox, nameHash string) (*corev1.Pod, error) {
Expand Down
38 changes: 38 additions & 0 deletions controllers/sandbox_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,7 @@ func TestReconcile(t *testing.T) {
Client: newFakeClient(append(tc.initialObjs, sb)...),
Scheme: Scheme,
Tracer: asmetrics.NewNoOp(),
ClusterDomain: "cluster.local",
}

_, err := r.Reconcile(t.Context(), ctrl.Request{
Expand Down Expand Up @@ -867,6 +868,7 @@ func TestReconcilePod(t *testing.T) {
Client: newFakeClient(append(tc.initialObjs, tc.sandbox)...),
Scheme: Scheme,
Tracer: asmetrics.NewNoOp(),
ClusterDomain: "cluster.local",
}

pod, err := r.reconcilePod(t.Context(), tc.sandbox, nameHash)
Expand Down Expand Up @@ -961,3 +963,39 @@ func TestSandboxExpiry(t *testing.T) {
})
}
}

func TestSetServiceStatusCustomDomain(t *testing.T) {
testCases := []struct {
name string
clusterDomain string
wantFQDN string
}{
{
name: "default cluster.local domain",
clusterDomain: "cluster.local",
wantFQDN: "my-svc.my-ns.svc.cluster.local",
},
{
name: "custom cluster domain",
clusterDomain: "custom.domain",
wantFQDN: "my-svc.my-ns.svc.custom.domain",
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
r := &SandboxReconciler{
ClusterDomain: tc.clusterDomain,
}
sandbox := &sandboxv1alpha1.Sandbox{}
service := &corev1.Service{}
service.Name = "my-svc"
service.Namespace = "my-ns"

r.setServiceStatus(sandbox, service)

require.Equal(t, "my-svc", sandbox.Status.Service)
require.Equal(t, tc.wantFQDN, sandbox.Status.ServiceFQDN)
})
}
}