Skip to content

Commit

Permalink
feat(appengine): surface dispatch rules on App Engine load balancers (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
danielpeach committed Apr 3, 2017
1 parent 749dce1 commit 656ab94
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 7 deletions.
Expand Up @@ -41,6 +41,8 @@ class AppengineLoadBalancer implements LoadBalancer, Serializable {
AppengineTrafficSplit split
String httpUrl
String httpsUrl
String project
List<AppenginePlatformApplication.AppengineDispatchRule> dispatchRules

AppengineLoadBalancer() { }

Expand All @@ -52,6 +54,8 @@ class AppengineLoadBalancer implements LoadBalancer, Serializable {
this.split = new ObjectMapper().convertValue(service.getSplit(), AppengineTrafficSplit)
this.httpUrl = AppengineModelUtil.getHttpUrl(service.getName())
this.httpsUrl = AppengineModelUtil.getHttpsUrl(service.getName())
// Self link has the form apps/{project}/services/{service}.
this.project = this.selfLink.split('/').getAt(1)
}

Void setLoadBalancerServerGroups(Set<AppengineServerGroup> serverGroups) {
Expand Down
Expand Up @@ -75,14 +75,13 @@ class AppengineModelUtil {
private static final String baseUrl = ".appspot.com"

/*
Self link has form apps/myapp/services/myservice/versions/myversion
Self link has the form apps/{project}/services/{service}/versions/{version}.
HTTPS: myversion-dot-myservice-dot-myapp.appspot.com
HTTP: myversion.myservice.myapp.appspot.com
This should work for services and versions, and for
the default service and its versions ("default" can be omitted from their URLs).
*/

@VisibleForTesting
static String getUrl(String selfLink, String delimiter) {
def parts = selfLink.split("/").reverse()
Expand Down
Expand Up @@ -29,6 +29,8 @@ class AppenginePlatformApplication {
String id
String locationId

AppenginePlatformApplication() {}

AppenginePlatformApplication(Application application) {
this.dispatchRules = application.getDispatchRules()?.collect { rule ->
new AppengineDispatchRule(rule.getDomain(), rule.getPath(), rule.getService())
Expand Down
Expand Up @@ -57,6 +57,9 @@ class AppengineLoadBalancerProvider implements LoadBalancerProvider<AppengineLoa
@Autowired
ObjectMapper objectMapper

@Autowired
AppenginePlatformApplicationProvider platformApplicationProvider

@Override
Set<AppengineLoadBalancer> getApplicationLoadBalancers(String applicationName) {
String applicationKey = Keys.getApplicationKey(applicationName)
Expand All @@ -65,12 +68,11 @@ class AppengineLoadBalancerProvider implements LoadBalancerProvider<AppengineLoa
def applicationLoadBalancers = AppengineProviderUtils.resolveRelationshipData(cacheView,
application,
Namespace.LOAD_BALANCERS.ns)
translateLoadBalancers(applicationLoadBalancers)
return translateLoadBalancers(applicationLoadBalancers)
}

Set<AppengineLoadBalancer> translateLoadBalancers(Collection<CacheData> cacheData) {
cacheData.collect { loadBalancerData ->

return cacheData.collect { loadBalancerData ->
Set<AppengineServerGroup> serverGroups = AppengineProviderUtils
.resolveRelationshipData(cacheView, loadBalancerData, Namespace.SERVER_GROUPS.ns)
.collect {
Expand All @@ -80,7 +82,11 @@ class AppengineLoadBalancerProvider implements LoadBalancerProvider<AppengineLoa
AppengineProviderUtils.serverGroupFromCacheData(objectMapper, it, instances)
}

AppengineProviderUtils.loadBalancerFromCacheData(objectMapper, loadBalancerData, serverGroups)
def loadBalancer = AppengineProviderUtils.loadBalancerFromCacheData(objectMapper, loadBalancerData, serverGroups)
def platformApplication = platformApplicationProvider.getPlatformApplication(loadBalancer.project)
loadBalancer.dispatchRules = platformApplication?.dispatchRules?.findAll { it.service == loadBalancer.name }

return loadBalancer
}
}

Expand All @@ -89,6 +95,6 @@ class AppengineLoadBalancerProvider implements LoadBalancerProvider<AppengineLoa
CacheData loadBalancerData = cacheView.get(Namespace.LOAD_BALANCERS.ns, loadBalancerKey)
Set<AppengineLoadBalancer> loadBalancerSet = translateLoadBalancers([loadBalancerData] - null)

loadBalancerSet ? loadBalancerSet.first() : null
return loadBalancerSet ? loadBalancerSet.first() : null
}
}
@@ -0,0 +1,44 @@
/*
* Copyright 2017 Google, Inc.
*
* 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 com.netflix.spinnaker.clouddriver.appengine.provider.view

import com.fasterxml.jackson.databind.ObjectMapper
import com.netflix.spinnaker.cats.cache.Cache
import com.netflix.spinnaker.clouddriver.appengine.cache.Keys
import com.netflix.spinnaker.clouddriver.appengine.model.AppenginePlatformApplication
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Component

@Component
class AppenginePlatformApplicationProvider {
@Autowired
Cache cacheView

@Autowired
ObjectMapper objectMapper

AppenginePlatformApplication getPlatformApplication(String project) {
def platformApplicationKey = Keys.getPlatformApplicationKey(project)
def platformApplicationData = cacheView.get(Keys.Namespace.PLATFORM_APPLICATIONS.ns, platformApplicationKey)

if (platformApplicationData) {
return objectMapper.convertValue(platformApplicationData.attributes.platformApplication, AppenginePlatformApplication)
} else {
return null
}
}
}

0 comments on commit 656ab94

Please sign in to comment.