Skip to content

Commit affb9ae

Browse files
committed
feat: Implement reconcile logic for Apisix resources and update status handling
1 parent 740eb1a commit affb9ae

13 files changed

+167
-96
lines changed

api/v2/apisixconsumer_types.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ type ApisixConsumerSpec struct {
2929
}
3030

3131
// ApisixConsumerStatus defines the observed state of ApisixConsumer.
32-
// +kubebuilder:deepcopy:generate=true
3332
type ApisixConsumerStatus = ApisixStatus
3433

3534
// +kubebuilder:object:root=true

api/v2/apisixglobalrule_types.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,7 @@ type ApisixGlobalRuleSpec struct {
2727
}
2828

2929
// ApisixGlobalRuleStatus defines the observed state of ApisixGlobalRule.
30-
// +kubebuilder:deepcopy:generate=true
31-
type ApisixGlobalRuleStatus struct {
32-
// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
33-
// Important: Run "make" to regenerate code after modifying this file
34-
}
30+
type ApisixGlobalRuleStatus = ApisixStatus
3531

3632
// +kubebuilder:object:root=true
3733
// +kubebuilder:subresource:status

api/v2/apisixpluginconfig_types.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ type ApisixPluginConfigSpec struct {
2828
}
2929

3030
// ApisixPluginConfigStatus defines the observed state of ApisixPluginConfig.
31-
// +kubebuilder:deepcopy:generate=true
3231
type ApisixPluginConfigStatus ApisixStatus
3332

3433
// +kubebuilder:object:root=true

api/v2/apisixroute_types.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ type ApisixRouteSpec struct {
2626
}
2727

2828
// ApisixRouteStatus defines the observed state of ApisixRoute.
29-
// +kubebuilder:deepcopy:generate=true
3029
type ApisixRouteStatus = ApisixStatus
3130

3231
// +kubebuilder:object:root=true

api/v2/apisixtls_types.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ type ApisixTlsSpec struct {
3232
}
3333

3434
// ApisixTlsStatus defines the observed state of ApisixTls.
35-
// +kubebuilder:deepcopy:generate=true
3635
type ApisixTlsStatus = ApisixStatus
3736

3837
// +kubebuilder:object:root=true

api/v2/apisixupstream_types.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ type ApisixUpstreamSpec struct {
3737
}
3838

3939
// ApisixUpstreamStatus defines the observed state of ApisixUpstream.
40-
// +kubebuilder:deepcopy:generate=true
4140
type ApisixUpstreamStatus = ApisixStatus
4241

4342
// +kubebuilder:object:root=true

api/v2/zz_generated.deepcopy.go

Lines changed: 1 addition & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/controller/apisixconsumer_controller.go

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,45 +15,59 @@ package controller
1515
import (
1616
"context"
1717

18+
"github.com/go-logr/logr"
19+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1820
"k8s.io/apimachinery/pkg/runtime"
1921
ctrl "sigs.k8s.io/controller-runtime"
2022
"sigs.k8s.io/controller-runtime/pkg/client"
21-
"sigs.k8s.io/controller-runtime/pkg/log"
23+
gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"
2224

23-
apisixapacheorgv2 "github.com/apache/apisix-ingress-controller/api/v2"
25+
apiv2 "github.com/apache/apisix-ingress-controller/api/v2"
2426
)
2527

2628
// ApisixConsumerReconciler reconciles a ApisixConsumer object
2729
type ApisixConsumerReconciler struct {
2830
client.Client
2931
Scheme *runtime.Scheme
32+
Log logr.Logger
3033
}
3134

3235
// +kubebuilder:rbac:groups=apisix.apache.org.github.com,resources=apisixconsumers,verbs=get;list;watch;create;update;patch;delete
3336
// +kubebuilder:rbac:groups=apisix.apache.org.github.com,resources=apisixconsumers/status,verbs=get;update;patch
3437
// +kubebuilder:rbac:groups=apisix.apache.org.github.com,resources=apisixconsumers/finalizers,verbs=update
3538

36-
// Reconcile is part of the main kubernetes reconciliation loop which aims to
37-
// move the current state of the cluster closer to the desired state.
38-
// TODO(user): Modify the Reconcile function to compare the state specified by
39-
// the ApisixConsumer object against the actual cluster state, and then
40-
// perform operations to make the cluster state reflect the state specified by
41-
// the user.
42-
//
43-
// For more details, check Reconcile and its Result here:
44-
// - https://pkg.go.dev/sigs.k8s.io/[email protected]/pkg/reconcile
39+
// Reconcile FIXME: implement the reconcile logic (For now, it dose nothing other than directly accepting)
4540
func (r *ApisixConsumerReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
46-
_ = log.FromContext(ctx)
41+
r.Log.Info("reconcile", "request", req.NamespacedName)
42+
43+
var obj apiv2.ApisixConsumer
44+
if err := r.Get(ctx, req.NamespacedName, &obj); err != nil {
45+
r.Log.Error(err, "failed to get ApisixConsumer", "request", req.NamespacedName)
46+
return ctrl.Result{}, err
47+
}
48+
49+
obj.Status.Conditions = []metav1.Condition{
50+
{
51+
Type: string(gatewayv1.RouteConditionAccepted),
52+
Status: metav1.ConditionTrue,
53+
ObservedGeneration: obj.GetGeneration(),
54+
LastTransitionTime: metav1.Now(),
55+
Reason: string(gatewayv1.RouteReasonAccepted),
56+
},
57+
}
4758

48-
// TODO(user): your logic here
59+
if err := r.Status().Update(ctx, &obj); err != nil {
60+
r.Log.Error(err, "failed to update status", "request", req.NamespacedName)
61+
return ctrl.Result{}, err
62+
}
4963

5064
return ctrl.Result{}, nil
5165
}
5266

5367
// SetupWithManager sets up the controller with the Manager.
5468
func (r *ApisixConsumerReconciler) SetupWithManager(mgr ctrl.Manager) error {
5569
return ctrl.NewControllerManagedBy(mgr).
56-
For(&apisixapacheorgv2.ApisixConsumer{}).
70+
For(&apiv2.ApisixConsumer{}).
5771
Named("apisixconsumer").
5872
Complete(r)
5973
}

internal/controller/apisixglobalrule_controller.go

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,45 +15,59 @@ package controller
1515
import (
1616
"context"
1717

18+
"github.com/go-logr/logr"
19+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1820
"k8s.io/apimachinery/pkg/runtime"
1921
ctrl "sigs.k8s.io/controller-runtime"
2022
"sigs.k8s.io/controller-runtime/pkg/client"
21-
"sigs.k8s.io/controller-runtime/pkg/log"
23+
gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"
2224

23-
apisixapacheorgv2 "github.com/apache/apisix-ingress-controller/api/v2"
25+
apiv2 "github.com/apache/apisix-ingress-controller/api/v2"
2426
)
2527

2628
// ApisixGlobalRuleReconciler reconciles a ApisixGlobalRule object
2729
type ApisixGlobalRuleReconciler struct {
2830
client.Client
2931
Scheme *runtime.Scheme
32+
Log logr.Logger
3033
}
3134

3235
// +kubebuilder:rbac:groups=apisix.apache.org.github.com,resources=apisixglobalrules,verbs=get;list;watch;create;update;patch;delete
3336
// +kubebuilder:rbac:groups=apisix.apache.org.github.com,resources=apisixglobalrules/status,verbs=get;update;patch
3437
// +kubebuilder:rbac:groups=apisix.apache.org.github.com,resources=apisixglobalrules/finalizers,verbs=update
3538

36-
// Reconcile is part of the main kubernetes reconciliation loop which aims to
37-
// move the current state of the cluster closer to the desired state.
38-
// TODO(user): Modify the Reconcile function to compare the state specified by
39-
// the ApisixGlobalRule object against the actual cluster state, and then
40-
// perform operations to make the cluster state reflect the state specified by
41-
// the user.
42-
//
43-
// For more details, check Reconcile and its Result here:
44-
// - https://pkg.go.dev/sigs.k8s.io/[email protected]/pkg/reconcile
39+
// Reconcile FIXME: implement the reconcile logic (For now, it dose nothing other than directly accepting)
4540
func (r *ApisixGlobalRuleReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
46-
_ = log.FromContext(ctx)
41+
r.Log.Info("reconcile", "request", req.NamespacedName)
42+
43+
var obj apiv2.ApisixGlobalRule
44+
if err := r.Get(ctx, req.NamespacedName, &obj); err != nil {
45+
r.Log.Error(err, "failed to get ApisixConsumer", "request", req.NamespacedName)
46+
return ctrl.Result{}, err
47+
}
48+
49+
obj.Status.Conditions = []metav1.Condition{
50+
{
51+
Type: string(gatewayv1.RouteConditionAccepted),
52+
Status: metav1.ConditionTrue,
53+
ObservedGeneration: obj.GetGeneration(),
54+
LastTransitionTime: metav1.Now(),
55+
Reason: string(gatewayv1.RouteReasonAccepted),
56+
},
57+
}
4758

48-
// TODO(user): your logic here
59+
if err := r.Status().Update(ctx, &obj); err != nil {
60+
r.Log.Error(err, "failed to update status", "request", req.NamespacedName)
61+
return ctrl.Result{}, err
62+
}
4963

5064
return ctrl.Result{}, nil
5165
}
5266

5367
// SetupWithManager sets up the controller with the Manager.
5468
func (r *ApisixGlobalRuleReconciler) SetupWithManager(mgr ctrl.Manager) error {
5569
return ctrl.NewControllerManagedBy(mgr).
56-
For(&apisixapacheorgv2.ApisixGlobalRule{}).
70+
For(&apiv2.ApisixGlobalRule{}).
5771
Named("apisixglobalrule").
5872
Complete(r)
5973
}

internal/controller/apisixroute_controller.go

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,45 +15,59 @@ package controller
1515
import (
1616
"context"
1717

18+
"github.com/go-logr/logr"
19+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1820
"k8s.io/apimachinery/pkg/runtime"
1921
ctrl "sigs.k8s.io/controller-runtime"
2022
"sigs.k8s.io/controller-runtime/pkg/client"
21-
"sigs.k8s.io/controller-runtime/pkg/log"
23+
gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"
2224

23-
apisixapacheorgv2 "github.com/apache/apisix-ingress-controller/api/v2"
25+
apiv2 "github.com/apache/apisix-ingress-controller/api/v2"
2426
)
2527

2628
// ApisixRouteReconciler reconciles a ApisixRoute object
2729
type ApisixRouteReconciler struct {
2830
client.Client
2931
Scheme *runtime.Scheme
32+
Log logr.Logger
3033
}
3134

3235
// +kubebuilder:rbac:groups=apisix.apache.org.github.com,resources=apisixroutes,verbs=get;list;watch;create;update;patch;delete
3336
// +kubebuilder:rbac:groups=apisix.apache.org.github.com,resources=apisixroutes/status,verbs=get;update;patch
3437
// +kubebuilder:rbac:groups=apisix.apache.org.github.com,resources=apisixroutes/finalizers,verbs=update
3538

36-
// Reconcile is part of the main kubernetes reconciliation loop which aims to
37-
// move the current state of the cluster closer to the desired state.
38-
// TODO(user): Modify the Reconcile function to compare the state specified by
39-
// the ApisixRoute object against the actual cluster state, and then
40-
// perform operations to make the cluster state reflect the state specified by
41-
// the user.
42-
//
43-
// For more details, check Reconcile and its Result here:
44-
// - https://pkg.go.dev/sigs.k8s.io/[email protected]/pkg/reconcile
4539
func (r *ApisixRouteReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
46-
_ = log.FromContext(ctx)
40+
r.Log.Info("reconcile", "request", req.NamespacedName)
41+
42+
var obj apiv2.ApisixRoute
43+
if err := r.Get(ctx, req.NamespacedName, &obj); err != nil {
44+
r.Log.Error(err, "failed to get ApisixConsumer", "request", req.NamespacedName)
45+
return ctrl.Result{}, err
46+
}
47+
48+
// FIXME: implement the reconcile logic (For now, it dose nothing other than directly accepting)
49+
obj.Status.Conditions = []metav1.Condition{
50+
{
51+
Type: string(gatewayv1.RouteConditionAccepted),
52+
Status: metav1.ConditionTrue,
53+
ObservedGeneration: obj.GetGeneration(),
54+
LastTransitionTime: metav1.Now(),
55+
Reason: string(gatewayv1.RouteReasonAccepted),
56+
},
57+
}
4758

48-
// TODO(user): your logic here
59+
if err := r.Status().Update(ctx, &obj); err != nil {
60+
r.Log.Error(err, "failed to update status", "request", req.NamespacedName)
61+
return ctrl.Result{}, err
62+
}
4963

5064
return ctrl.Result{}, nil
5165
}
5266

5367
// SetupWithManager sets up the controller with the Manager.
5468
func (r *ApisixRouteReconciler) SetupWithManager(mgr ctrl.Manager) error {
5569
return ctrl.NewControllerManagedBy(mgr).
56-
For(&apisixapacheorgv2.ApisixRoute{}).
70+
For(&apiv2.ApisixRoute{}).
5771
Named("apisixroute").
5872
Complete(r)
5973
}

0 commit comments

Comments
 (0)