Skip to content

Commit

Permalink
Correctly serialize and deserialize labels and annotations
Browse files Browse the repository at this point in the history
Labels and annotations need to be serialized under the `metadata` key.
  • Loading branch information
jakobfelsatdm committed Oct 9, 2023
1 parent 434ac05 commit d65b446
Show file tree
Hide file tree
Showing 8 changed files with 928 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,19 @@ private static ManifestV3Application.Builder toApplicationManifest(Map<String, O
asList(application, "processes", variables, raw -> getProcess((Map<String, Object>) raw, variables), builder::processe);
asList(application, "services", variables, raw -> getService(raw, variables), builder::service);
asList(application, "sidecars", variables, raw -> getSidecar((Map<String, Object>) raw, variables), builder::sidecar);
as(application, "labels", variables, Map.class::cast, builder::labels);
as(application, "annotations", variables, Map.class::cast, builder::annotations);
as(application, "metadata", variables, raw -> getMetadata((Map<String, Object>)raw, variables), builder::metadata);
asBoolean(application, "default-route", variables, builder::defaultRoute);

return builder;
}

private static ManifestV3Metadata getMetadata(Map<String, Object> raw, Map<String, String> variables) {
ManifestV3Metadata.Builder builder = ManifestV3Metadata.builder();
asMapOfStringString(raw, "labels", variables, builder::label);
asMapOfStringString(raw, "annotations", variables, builder::annotation);
return builder.build();
}

private static ManifestV3Sidecar getSidecar(Map<String, Object> raw, Map<String, String> variables) {
ManifestV3Sidecar.Builder builder = ManifestV3Sidecar.builder();

Expand Down Expand Up @@ -210,8 +216,7 @@ private static Map<String, Object> toApplicationYaml(ManifestV3Application appli
putIfPresent(yaml, "default-route", application.getDefaultRoute());
putIfPresent(yaml, "services", convertCollection(application.getServices(), ApplicationManifestUtilsV3::toServiceYaml));
putIfPresent(yaml, "sidecars", convertCollection(application.getSidecars(), ApplicationManifestUtilsV3::toSidecarsYaml));
putIfPresent(yaml, "labels", application.getLabels());
putIfPresent(yaml, "annotations", application.getAnnotations());
putIfPresent(yaml, "metadata", application.getMetadata(), ApplicationManifestUtilsV3::toMetadataYaml);
return yaml;
}

Expand Down Expand Up @@ -248,4 +253,12 @@ private static Map<String, Object> toProcessYaml(ManifestV3Process process) {
putIfPresent(yaml, "timeout", process.getTimeout());
return yaml;
}

private static Map<String, Object> toMetadataYaml(ManifestV3Metadata metadata) {
if (metadata == null) return null;
Map<String, Object> yaml = new TreeMap<>();
putIfPresent(yaml, "annotations", metadata.getAnnotations());
putIfPresent(yaml, "labels", metadata.getLabels());
return yaml;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,16 @@
abstract class _ManifestV3Application extends _ApplicationManifestCommon {

/**
* The annotations configured for this application
* The metadata configured for this application. May contain labels and annotations
*/
@AllowNulls
@Nullable
abstract Map<String, String> getAnnotations();

abstract ManifestV3Metadata getMetadata();
/**
* Generate a default route based on the application name
*/
@Nullable
abstract Boolean getDefaultRoute();

/**
* The labels configured for this application
*/
@AllowNulls
@Nullable
abstract Map<String, String> getLabels();

/**
* The collection of processes configured for this application
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2013-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.cloudfoundry.operations.applications;


import org.cloudfoundry.AllowNulls;
import org.cloudfoundry.Nullable;
import org.immutables.value.Value;

import java.util.Map;


@Value.Immutable
abstract class _ManifestV3Metadata {

@AllowNulls
@Nullable
abstract Map<String, String> getAnnotations();

@AllowNulls
@Nullable
abstract Map<String, String> getLabels();
}
Loading

0 comments on commit d65b446

Please sign in to comment.