Skip to content

Commit

Permalink
handle conflict if resource already exists
Browse files Browse the repository at this point in the history
Signed-off-by: Troy Connor <troy0820@users.noreply.github.com>
  • Loading branch information
troy0820 committed Jun 26, 2024
1 parent 2c48935 commit 7d396e1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 26 deletions.
10 changes: 9 additions & 1 deletion controllers/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ var GrpcDeployment = &appsv1.Deployment{
},
},
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
"app": "porter-grpc-service",
},
},
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Expand Down Expand Up @@ -78,6 +83,9 @@ var GrpcDeployment = &appsv1.Deployment{
Name: "porter-grpc-service-config-volume",
VolumeSource: corev1.VolumeSource{
ConfigMap: &corev1.ConfigMapVolumeSource{
LocalObjectReference: corev1.LocalObjectReference{
Name: "porter-grpc-service-config",
},
Items: []corev1.KeyToPath{
{
Key: "config",
Expand Down Expand Up @@ -105,7 +113,7 @@ var GrpcService = &corev1.Service{
Ports: []corev1.ServicePort{
{
Protocol: corev1.ProtocolTCP,
TargetPort: intstr.FromString("3001"),
TargetPort: intstr.FromInt(3001),
Port: int32(3001),
},
},
Expand Down
40 changes: 15 additions & 25 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,8 @@ import (
_ "k8s.io/client-go/plugin/pkg/client/auth"

"golang.org/x/sync/errgroup"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
ctrl "sigs.k8s.io/controller-runtime"
Expand Down Expand Up @@ -131,44 +128,37 @@ func main() {
g, ctx := errgroup.WithContext(ctx)
g.Go(func() error {
k8sClient := mgr.GetClient()
// GET deployment to see if it exists first before creating it
deployment := &appsv1.Deployment{}
err := k8sClient.Get(ctx, types.NamespacedName{Name: controllers.GrpcDeployment.Name, Namespace: controllers.GrpcDeployment.Namespace}, deployment)
err := k8sClient.Create(ctx, controllers.GrpcConfigMap, &client.CreateOptions{})
if err != nil {
if apierrors.IsNotFound(err) {
setupLog.Info("creating grpc deployment")
return k8sClient.Create(ctx, controllers.GrpcDeployment, &client.CreateOptions{})
if apierrors.IsAlreadyExists(err) {
setupLog.Error(err, "configmap already exists, not creating")
return nil
}
setupLog.Error(err, "error creating configmap")
}
// NOTE: Don't crash, just don't deploy if Get fails for any other reason than not found
return nil
})

g.Go(func() error {
k8sClient := mgr.GetClient()
service := &corev1.Service{}
err := k8sClient.Get(ctx, types.NamespacedName{Name: controllers.GrpcService.Name, Namespace: controllers.GrpcService.Namespace}, service)
err = k8sClient.Create(ctx, controllers.GrpcDeployment, &client.CreateOptions{})
if err != nil {
if apierrors.IsNotFound(err) {
setupLog.Info("creating grpc service")
return k8sClient.Create(ctx, controllers.GrpcService, &client.CreateOptions{})
if apierrors.IsAlreadyExists(err) {
setupLog.Error(err, "deployment already exists, not creating")
return nil
}
setupLog.Error(err, "error creating configmap")
}
// NOTE: Don't crash, just don't deploy if Get fails for any other reason than not found
return nil
})

g.Go(func() error {
k8sClient := mgr.GetClient()
cm := &corev1.ConfigMap{}
err := k8sClient.Get(ctx, types.NamespacedName{Name: controllers.GrpcConfigMap.Name, Namespace: controllers.GrpcConfigMap.Namespace}, cm)
err := k8sClient.Create(ctx, controllers.GrpcService, &client.CreateOptions{})
if err != nil {
if apierrors.IsNotFound(err) {
setupLog.Info("creating grpc configmap")
return k8sClient.Create(ctx, controllers.GrpcConfigMap, &client.CreateOptions{})
if apierrors.IsAlreadyExists(err) {
setupLog.Error(err, "service already exists, not creating")
return nil
}
setupLog.Error(err, "error creating service")
}
// NOTE: Don't crash, just don't deploy if Get fails for any other reason than not found
return nil
})

Expand Down

0 comments on commit 7d396e1

Please sign in to comment.