Skip to content

Commit

Permalink
Merge pull request apolloconfig#499 from lepdou/1221
Browse files Browse the repository at this point in the history
return default cluster's namespace when custom cluster's namespace ne…
  • Loading branch information
nobodyiam authored Dec 26, 2016
2 parents bae4505 + 33ad494 commit b9057d2
Show file tree
Hide file tree
Showing 9 changed files with 220 additions and 182 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class NamespaceController {

@RequestMapping(path = "/apps/{appId}/clusters/{clusterName}/namespaces", method = RequestMethod.POST)
public NamespaceDTO create(@PathVariable("appId") String appId,
@PathVariable("clusterName") String clusterName, @RequestBody NamespaceDTO dto) {
@PathVariable("clusterName") String clusterName, @RequestBody NamespaceDTO dto) {
if (!InputValidator.isValidClusterNamespace(dto.getNamespaceName())) {
throw new BadRequestException(String.format("Namespace格式错误: %s", InputValidator.INVALID_CLUSTER_NAMESPACE_MESSAGE));
}
Expand All @@ -45,18 +45,18 @@ public NamespaceDTO create(@PathVariable("appId") String appId,

@RequestMapping(path = "/apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName:.+}", method = RequestMethod.DELETE)
public void delete(@PathVariable("appId") String appId,
@PathVariable("clusterName") String clusterName,
@PathVariable("namespaceName") String namespaceName, @RequestParam String operator) {
@PathVariable("clusterName") String clusterName,
@PathVariable("namespaceName") String namespaceName, @RequestParam String operator) {
Namespace entity = namespaceService.findOne(appId, clusterName, namespaceName);
if (entity == null) throw new NotFoundException(
String.format("namespace not found for %s %s %s", appId, clusterName, namespaceName));
String.format("namespace not found for %s %s %s", appId, clusterName, namespaceName));

namespaceService.deleteNamespace(entity, operator);
}

@RequestMapping("/apps/{appId}/clusters/{clusterName}/namespaces")
public List<NamespaceDTO> find(@PathVariable("appId") String appId,
@PathVariable("clusterName") String clusterName) {
@PathVariable("clusterName") String clusterName) {
List<Namespace> groups = namespaceService.findNamespaces(appId, clusterName);
return BeanUtils.batchTransform(NamespaceDTO.class, groups);
}
Expand All @@ -71,18 +71,19 @@ public NamespaceDTO get(@PathVariable("namespaceId") Long namespaceId) {

@RequestMapping("/apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName:.+}")
public NamespaceDTO get(@PathVariable("appId") String appId,
@PathVariable("clusterName") String clusterName,
@PathVariable("namespaceName") String namespaceName) {
@PathVariable("clusterName") String clusterName,
@PathVariable("namespaceName") String namespaceName) {
Namespace namespace = namespaceService.findOne(appId, clusterName, namespaceName);
if (namespace == null) throw new NotFoundException(
String.format("namespace not found for %s %s %s", appId, clusterName, namespaceName));
String.format("namespace not found for %s %s %s", appId, clusterName, namespaceName));
return BeanUtils.transfrom(NamespaceDTO.class, namespace);
}

@RequestMapping("/clusters/{clusterName}/namespaces/{namespaceName}/public")
public NamespaceDTO findPublicNamespace(@PathVariable String clusterName,
@PathVariable String namespaceName) {
Namespace namespace = namespaceService.findPublicNamespace(clusterName, namespaceName);
@RequestMapping("/apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName}/associated-public-namespace")
public NamespaceDTO findPublicNamespaceForAssociatedNamespace(@PathVariable String appId,
@PathVariable String clusterName,
@PathVariable String namespaceName) {
Namespace namespace = namespaceService.findPublicNamespaceForAssociatedNamespace(clusterName, namespaceName);

if (namespace == null) {
throw new NotFoundException(String.format("public namespace not found. namespace:%s", namespaceName));
Expand All @@ -95,7 +96,7 @@ public NamespaceDTO findPublicNamespace(@PathVariable String clusterName,
* cluster -> cluster has not published namespaces?
*/
@RequestMapping("/apps/{appId}/namespaces/publish_info")
public Map<String, Boolean> namespacePublishInfo(@PathVariable String appId){
public Map<String, Boolean> namespacePublishInfo(@PathVariable String appId) {
return namespaceService.namespacePublishInfo(appId);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ public class NamespaceService {
private InstanceService instanceService;



public Namespace findOne(Long namespaceId) {
return namespaceRepository.findOne(namespaceId);
}
Expand All @@ -69,7 +68,7 @@ public Namespace findOne(String appId, String clusterName, String namespaceName)
namespaceName);
}

public Namespace findPublicNamespace(String clusterName, String namespaceName) {
public Namespace findPublicNamespaceForAssociatedNamespace(String clusterName, String namespaceName) {
AppNamespace appNamespace = appNamespaceService.findPublicNamespaceByName(namespaceName);
if (appNamespace == null) {
throw new BadRequestException("namespace not exist");
Expand All @@ -79,11 +78,46 @@ public Namespace findPublicNamespace(String clusterName, String namespaceName) {

Namespace namespace = findOne(appId, clusterName, namespaceName);

//default cluster's namespace
if (Objects.equals(clusterName, ConfigConsts.CLUSTER_NAME_DEFAULT)) {
return namespace;
}

//custom cluster's namespace not exist.
//return default cluster's namespace
if (namespace == null) {
namespace = findOne(appId, ConfigConsts.CLUSTER_NAME_DEFAULT, namespaceName);
return findOne(appId, ConfigConsts.CLUSTER_NAME_DEFAULT, namespaceName);
}

//custom cluster's namespace exist and has published.
//return custom cluster's namespace
Release latestActiveRelease = releaseService.findLatestActiveRelease(namespace);
if (latestActiveRelease != null) {
return namespace;
}

Namespace defaultNamespace = findOne(appId, ConfigConsts.CLUSTER_NAME_DEFAULT, namespaceName);

//custom cluster's namespace exist but never published.
//and default cluster's namespace not exist.
//return custom cluster's namespace
if (defaultNamespace == null) {
return namespace;
}

//custom cluster's namespace exist but never published.
//and default cluster's namespace exist and has published.
//return default cluster's namespace
Release defaultNamespaceLatestActiveRelease = releaseService.findLatestActiveRelease(defaultNamespace);
if (defaultNamespaceLatestActiveRelease != null) {
return defaultNamespace;
}

//custom cluster's namespace exist but never published.
//and default cluster's namespace exist but never published.
//return custom cluster's namespace
return namespace;

}

public List<Namespace> findNamespaces(String appId, String clusterName) {
Expand Down Expand Up @@ -129,7 +163,7 @@ public Namespace findChildNamespace(Namespace parentNamespace) {

}

public Namespace findParentNamespace(String appId, String clusterName, String namespaceName){
public Namespace findParentNamespace(String appId, String clusterName, String namespaceName) {
return findParentNamespace(new Namespace(appId, clusterName, namespaceName));
}

Expand All @@ -146,7 +180,7 @@ public Namespace findParentNamespace(Namespace namespace) {
return null;
}

public boolean isChildNamespace(String appId, String clusterName, String namespaceName){
public boolean isChildNamespace(String appId, String clusterName, String namespaceName) {
return isChildNamespace(new Namespace(appId, clusterName, namespaceName));
}

Expand Down Expand Up @@ -268,10 +302,10 @@ public Map<String, Boolean> namespacePublishInfo(String appId) {
String clusterName = cluster.getName();
List<Namespace> namespaces = findNamespaces(appId, clusterName);

for (Namespace namespace: namespaces) {
for (Namespace namespace : namespaces) {
boolean isNamespaceNotPublished = isNamespaceNotPublished(namespace);

if (isNamespaceNotPublished){
if (isNamespaceNotPublished) {
clusterHasNotPublishedItems.put(clusterName, true);
break;
}
Expand Down Expand Up @@ -301,8 +335,8 @@ private boolean isNamespaceNotPublished(Namespace namespace) {
}

Map<String, String> publishedConfiguration = gson.fromJson(latestRelease.getConfigurations(), GsonType.CONFIG);
for (Item item: itemsModifiedAfterLastPublish) {
if (!Objects.equals(item.getValue(), publishedConfiguration.get(item.getKey()))){
for (Item item : itemsModifiedAfterLastPublish) {
if (!Objects.equals(item.getValue(), publishedConfiguration.get(item.getKey()))) {
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,10 @@ public NamespaceDTO loadNamespace(String appId, Env env, String clusterName,
NamespaceDTO.class, appId, clusterName, namespaceName);
}

public NamespaceDTO loadPublicNamespace(Env env, String clusterName, String namespaceName) {
public NamespaceDTO findPublicNamespaceForAssociatedNamespace(Env env, String appId, String clusterName, String namespaceName) {
return
restTemplate.get(env, "/clusters/{clusterName}/namespaces/{namespaceName}/public",
NamespaceDTO.class, clusterName, namespaceName);
restTemplate.get(env, "apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName}/associated-public-namespace",
NamespaceDTO.class, appId, clusterName, namespaceName);
}

public NamespaceDTO createNamespace(Env env, NamespaceDTO namespace) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public abstract class ConfigPublishEmailBuilder {
//set config's value max length to protect email.
protected static final int VALUE_MAX_LENGTH = 100;

private FastDateFormat dateFormat = FastDateFormat.getInstance("yyyy-MM-dd hh:mm:ss");
protected FastDateFormat dateFormat = FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss");


@Autowired
Expand Down
Loading

0 comments on commit b9057d2

Please sign in to comment.