Skip to content

Commit

Permalink
fix(proxy-directory): deserialize response manually
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremylvln committed Mar 5, 2022
1 parent 655e052 commit 1b45563
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 19 deletions.
2 changes: 1 addition & 1 deletion internal/resource/cluster/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func (b *MinecraftClusterResourceBuilder) ResourceBuilders() ([]common.ResourceB
}

func (b *MinecraftClusterResourceBuilder) getRoleName() string {
return fmt.Sprintf("%s-cluster-status-watch", b.Instance.Name)
return fmt.Sprintf("%s-cluster-watch", b.Instance.Name)
}

func (b *MinecraftClusterResourceBuilder) getLabels() map[string]string {
Expand Down
2 changes: 1 addition & 1 deletion internal/resource/cluster/role.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (b *MinecraftClusterRoleBuilder) Update(object client.Object) error {
role.Rules = []rbacv1.PolicyRule{
{
APIGroups: []string{"shulkermc.io"},
Resources: []string{"minecraftclusters/status"},
Resources: []string{"minecraftclusters"},
Verbs: []string{"get", "watch"},
ResourceNames: []string{b.Instance.Name},
},
Expand Down
2 changes: 1 addition & 1 deletion internal/resource/proxy/rolebinding.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (b *ProxyDeploymentRoleBindingBuilder) Build() (client.Object, error) {
RoleRef: rbacv1.RoleRef{
APIGroup: "rbac.authorization.k8s.io",
Kind: "Role",
Name: fmt.Sprintf("%s-cluster-status-watch", b.Cluster.Name),
Name: fmt.Sprintf("%s-cluster-watch", b.Cluster.Name),
},
}, nil
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package io.shulkermc.directory;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.reflect.TypeToken;
import io.kubernetes.client.openapi.ApiClient;
import io.kubernetes.client.openapi.ApiException;
import io.kubernetes.client.openapi.Configuration;
import io.kubernetes.client.openapi.JSON;
import io.kubernetes.client.openapi.apis.CustomObjectsApi;
import io.kubernetes.client.util.ClientBuilder;
import io.kubernetes.client.util.ModelMapper;
import io.kubernetes.client.util.Watch;
import io.kubernetes.client.util.generic.GenericKubernetesApi;
import io.shulkermc.models.V1alpha1MinecraftCluster;
import io.shulkermc.models.V1alpha1MinecraftClusterStatus;
import io.shulkermc.models.V1alpha1MinecraftClusterStatusServerPool;
Expand Down Expand Up @@ -50,7 +51,7 @@ public void onEnable() {
return;
}

this.getLogger().info(String.format("Shulker cluster name is \"%s\"", this.shulkerClusterName));
this.getLogger().info(String.format("Shulker cluster is \"%s/%s\"", this.shulkerClusterNamespace, this.shulkerClusterName));

ApiClient kubernetesClient;
try {
Expand All @@ -70,9 +71,9 @@ public void onEnable() {
while (this.reconcilerContinue.get()) {
try {
this.getLogger().info("Reconciling cluster status");
Watch<V1alpha1MinecraftCluster> watch = Watch.createWatch(
Watch<Object> watch = Watch.createWatch(
kubernetesClient,
customObjectsApi.getNamespacedCustomObjectCall(
customObjectsApi.getNamespacedCustomObjectStatusCall(
"shulkermc.io",
"v1alpha1",
this.shulkerClusterNamespace,
Expand All @@ -84,9 +85,9 @@ public void onEnable() {

for (var event : watch) {
if (!event.type.equals("MODIFIED")) continue;
V1alpha1MinecraftCluster cluster = event.object;
if (cluster.getStatus() != null) continue;
this.updateServerDirectory(cluster.getStatus().getServerPool());
Object object = event.object;
V1alpha1MinecraftClusterStatus status = ShulkerProxyDirectory.responseToStatusObject(object);
this.updateServerDirectory(status.getServerPool());
}
} catch (Exception ex) {
this.getLogger().severe("Failed to watch cluster status");
Expand All @@ -101,12 +102,9 @@ public void onEnable() {
this.reconcilerThread.start();

try {
V1alpha1MinecraftCluster cluster = (V1alpha1MinecraftCluster) customObjectsApi.getNamespacedCustomObject(
"shulkermc.io", "v1alpha1", this.shulkerClusterNamespace, "minecraftclusters", this.shulkerClusterName);

if (cluster.getStatus() != null) {
this.updateServerDirectory(cluster.getStatus().getServerPool());
}
V1alpha1MinecraftClusterStatus status = ShulkerProxyDirectory.responseToStatusObject(customObjectsApi.getNamespacedCustomObjectStatus(
"shulkermc.io", "v1alpha1", this.shulkerClusterNamespace, "minecraftclusters", this.shulkerClusterName));
this.updateServerDirectory(status.getServerPool());
} catch (ApiException ex) {
this.getLogger().severe("Failed to synchronize server directory");
ex.printStackTrace();
Expand Down Expand Up @@ -147,7 +145,10 @@ private void updateServerDirectory(List<V1alpha1MinecraftClusterStatusServerPool
.forEach(proxyServers::remove);
}

static {
ModelMapper.addModelMap("shulkermc.io", "v1alpha1", "MinecraftCluster", "minecraftclusters", V1alpha1MinecraftCluster.class);
private static V1alpha1MinecraftClusterStatus responseToStatusObject(Object o) {
if (o == null) return null;
Gson gson = new JSON().getGson();
JsonElement jsonElement = gson.toJsonTree(o);
return gson.fromJson(jsonElement, V1alpha1MinecraftClusterStatus.class);
}
}

0 comments on commit 1b45563

Please sign in to comment.