-
Notifications
You must be signed in to change notification settings - Fork 637
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(plugins): Plugin version pinning (#791)
Adds new metadata store for pinning plugin versions to a server group. This ensures a server group will continue to load the same plugins even through instance replacement on long-lived server groups. Metadata is regularly cleaned up, leaving the last N records for a particular cluster, by default leaving the last 10 (which is likely excessive).
- Loading branch information
1 parent
152ec5f
commit d4d33ed
Showing
29 changed files
with
692 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
.../src/main/groovy/com/netflix/spinnaker/front50/config/PluginVersionCleanupProperties.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* Copyright 2020 Netflix, 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.front50.config; | ||
|
||
import java.time.Duration; | ||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
|
||
/** Controls configuration for the plugin version pinning metadata storage cleanup. */ | ||
@ConfigurationProperties("storage-service.plugin-version-pinning.cleanup") | ||
public class PluginVersionCleanupProperties { | ||
/** The maximum number of pinned version records to keep by cluster (and location). */ | ||
public int maxVersionsPerCluster = 10; | ||
|
||
/** The interval that the cleanup agent will run. */ | ||
public Duration interval = Duration.ofDays(1); | ||
} |
47 changes: 47 additions & 0 deletions
47
...e/src/main/groovy/com/netflix/spinnaker/front50/config/PluginVersioningConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Copyright 2020 Netflix, 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.front50.config; | ||
|
||
import com.netflix.spinnaker.front50.model.plugins.PluginInfoRepository; | ||
import com.netflix.spinnaker.front50.model.plugins.PluginVersionCleanupAgent; | ||
import com.netflix.spinnaker.front50.model.plugins.PluginVersionPinningRepository; | ||
import com.netflix.spinnaker.front50.model.plugins.PluginVersionPinningService; | ||
import com.netflix.spinnaker.moniker.Namer; | ||
import org.springframework.boot.context.properties.EnableConfigurationProperties; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.scheduling.TaskScheduler; | ||
|
||
@Configuration | ||
@EnableConfigurationProperties(PluginVersionCleanupProperties.class) | ||
public class PluginVersioningConfiguration { | ||
|
||
@Bean | ||
PluginVersionPinningService pluginVersionPinningService( | ||
PluginVersionPinningRepository pluginVersionPinningRepository, | ||
PluginInfoRepository pluginInfoRepository) { | ||
return new PluginVersionPinningService(pluginVersionPinningRepository, pluginInfoRepository); | ||
} | ||
|
||
@Bean | ||
PluginVersionCleanupAgent pluginVersionCleanupAgent( | ||
PluginVersionPinningRepository repository, | ||
PluginVersionCleanupProperties properties, | ||
Namer<?> namer, | ||
TaskScheduler taskScheduler) { | ||
return new PluginVersionCleanupAgent(repository, properties, namer, taskScheduler); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
...vy/com/netflix/spinnaker/front50/model/plugins/DefaultPluginVersionPinningRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* Copyright 2020 Netflix, 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.front50.model.plugins; | ||
|
||
import com.netflix.spectator.api.Registry; | ||
import com.netflix.spinnaker.front50.model.ObjectKeyLoader; | ||
import com.netflix.spinnaker.front50.model.ObjectType; | ||
import com.netflix.spinnaker.front50.model.StorageService; | ||
import com.netflix.spinnaker.front50.model.StorageServiceSupport; | ||
import com.netflix.spinnaker.kork.exceptions.IntegrationException; | ||
import java.util.Objects; | ||
import rx.Scheduler; | ||
|
||
public class DefaultPluginVersionPinningRepository | ||
extends StorageServiceSupport<ServerGroupPluginVersions> | ||
implements PluginVersionPinningRepository { | ||
public DefaultPluginVersionPinningRepository( | ||
StorageService service, | ||
Scheduler scheduler, | ||
ObjectKeyLoader objectKeyLoader, | ||
long refreshIntervalMs, | ||
boolean shouldWarmCache, | ||
Registry registry) { | ||
super( | ||
ObjectType.PLUGIN_VERSIONS, | ||
service, | ||
scheduler, | ||
objectKeyLoader, | ||
refreshIntervalMs, | ||
shouldWarmCache, | ||
registry); | ||
} | ||
|
||
@Override | ||
public ServerGroupPluginVersions create(String id, ServerGroupPluginVersions item) { | ||
Objects.requireNonNull(item.getId()); | ||
Objects.requireNonNull(item.getPluginVersions()); | ||
if (!item.getId().equals(id)) { | ||
throw new IntegrationException("The provided id and body id do not match"); | ||
} | ||
|
||
if (item.getCreateTs() == null) { | ||
item.setCreateTs(System.currentTimeMillis()); | ||
} | ||
item.setLastModified(System.currentTimeMillis()); | ||
|
||
update(id, item); | ||
return findById(id); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
...rc/main/groovy/com/netflix/spinnaker/front50/model/plugins/PluginVersionCleanupAgent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/* | ||
* Copyright 2020 Netflix, 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.front50.model.plugins; | ||
|
||
import static java.lang.String.format; | ||
|
||
import com.netflix.spinnaker.front50.config.PluginVersionCleanupProperties; | ||
import com.netflix.spinnaker.moniker.Namer; | ||
import java.util.Collection; | ||
import java.util.Comparator; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import javax.annotation.PostConstruct; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.scheduling.TaskScheduler; | ||
|
||
/** Responsible for cleaning up old server group plugin version records. */ | ||
public class PluginVersionCleanupAgent implements Runnable { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(PluginVersionCleanupAgent.class); | ||
|
||
private final PluginVersionPinningRepository repository; | ||
private final PluginVersionCleanupProperties properties; | ||
private final Namer namer; | ||
private final TaskScheduler taskScheduler; | ||
|
||
public PluginVersionCleanupAgent( | ||
PluginVersionPinningRepository repository, | ||
PluginVersionCleanupProperties properties, | ||
Namer namer, | ||
TaskScheduler taskScheduler) { | ||
this.repository = repository; | ||
this.properties = properties; | ||
this.namer = namer; | ||
this.taskScheduler = taskScheduler; | ||
} | ||
|
||
@PostConstruct | ||
public void schedule() { | ||
taskScheduler.scheduleWithFixedDelay(this, properties.interval); | ||
} | ||
|
||
@Override | ||
public void run() { | ||
log.info("Starting cleanup"); | ||
|
||
Collection<ServerGroupPluginVersions> allVersions = repository.all(); | ||
|
||
// Group all versions by cluster & location (region), then reduce the list by groups that have | ||
// more than maxVersionsPerCluster, deleting the oldest server group records by created | ||
// timestamp. | ||
allVersions.stream() | ||
.collect( | ||
Collectors.groupingBy( | ||
it -> { | ||
String clusterName = namer.deriveMoniker(it.getServerGroupName()).getCluster(); | ||
String group = format("%s-%s", clusterName, it.getLocation()); | ||
return group; | ||
})) | ||
.entrySet() | ||
.stream() | ||
.filter(it -> it.getValue().size() > properties.maxVersionsPerCluster) | ||
.forEach( | ||
it -> { | ||
List<String> candidates = | ||
it.getValue().stream() | ||
.sorted(Comparator.comparing(ServerGroupPluginVersions::getCreateTs)) | ||
.sorted(Comparator.reverseOrder()) | ||
.map(ServerGroupPluginVersions::getId) | ||
.collect(Collectors.toList()); | ||
|
||
List<String> ids = | ||
candidates.subList(properties.maxVersionsPerCluster, candidates.size()); | ||
|
||
log.debug("Deleting {} version records for '{}'", ids.size(), it.getKey()); | ||
repository.bulkDelete(ids); | ||
}); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
...in/groovy/com/netflix/spinnaker/front50/model/plugins/PluginVersionPinningRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/* | ||
* Copyright 2020 Netflix, 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.front50.model.plugins; | ||
|
||
import com.netflix.spinnaker.front50.model.ItemDAO; | ||
|
||
public interface PluginVersionPinningRepository extends ItemDAO<ServerGroupPluginVersions> {} |
Oops, something went wrong.