-
Notifications
You must be signed in to change notification settings - Fork 3.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
xds: Parsing xDS Cluster Metadata #11741
Changes from 4 commits
a66f3c0
02e346a
9614cb6
c51fa74
e028355
33009b2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* Copyright 2024 The gRPC 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 io.grpc.xds; | ||
|
||
import com.google.common.annotations.VisibleForTesting; | ||
import com.google.protobuf.Any; | ||
import com.google.protobuf.InvalidProtocolBufferException; | ||
import io.grpc.xds.GcpAuthenticationFilter.AudienceMetadataParser; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import javax.annotation.Nullable; | ||
|
||
/** | ||
* Registry for parsing cluster metadata values. | ||
* | ||
* <p>This class maintains a mapping of type URLs to {@link ClusterMetadataValueParser} instances, | ||
* allowing for the parsing of different metadata types. | ||
*/ | ||
final class ClusterMetadataRegistry { | ||
private static final ClusterMetadataRegistry INSTANCE = new ClusterMetadataRegistry(); | ||
|
||
private final Map<String, ClusterMetadataValueParser> supportedParsers = new HashMap<>(); | ||
|
||
private ClusterMetadataRegistry() { | ||
registerParser(new AudienceMetadataParser()); | ||
} | ||
|
||
static ClusterMetadataRegistry getInstance() { | ||
return INSTANCE; | ||
} | ||
|
||
@Nullable | ||
ClusterMetadataValueParser findParser(String typeUrl) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should have a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
return supportedParsers.get(typeUrl); | ||
} | ||
|
||
@VisibleForTesting | ||
void registerParser(ClusterMetadataValueParser parser) { | ||
supportedParsers.put(parser.getTypeUrl(), parser); | ||
} | ||
|
||
void removeParser(ClusterMetadataValueParser parser) { | ||
supportedParsers.remove(parser.getTypeUrl()); | ||
} | ||
|
||
interface ClusterMetadataValueParser { | ||
|
||
String getTypeUrl(); | ||
|
||
/** | ||
* Parses the given {@link Any} object into a specific metadata value. | ||
* | ||
* @param any the {@link Any} object to parse. | ||
* @return the parsed metadata value. | ||
* @throws InvalidProtocolBufferException if the parsing fails. | ||
*/ | ||
Object parse(Any any) throws InvalidProtocolBufferException; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* Copyright 2024 The gRPC 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 io.grpc.xds.internal; | ||
|
||
import com.google.protobuf.Struct; | ||
import com.google.protobuf.Value; | ||
import io.grpc.Internal; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
import javax.annotation.Nullable; | ||
|
||
/** | ||
* Converter for Protobuf {@link Struct} to JSON-like {@link Map}. | ||
*/ | ||
@Internal | ||
public final class ProtobufJsonConverter { | ||
private ProtobufJsonConverter() {} | ||
|
||
public static Map<String, Object> convertToJson(Struct struct) { | ||
Map<String, Object> result = new HashMap<>(); | ||
for (Map.Entry<String, Value> entry : struct.getFieldsMap().entrySet()) { | ||
result.put(entry.getKey(), convertValue(entry.getValue())); | ||
} | ||
return result; | ||
} | ||
|
||
@Nullable | ||
private static Object convertValue(Value value) { | ||
switch (value.getKindCase()) { | ||
case STRUCT_VALUE: | ||
return convertToJson(value.getStructValue()); | ||
case LIST_VALUE: | ||
return value.getListValue().getValuesList().stream() | ||
.map(ProtobufJsonConverter::convertValue) | ||
.collect(Collectors.toList()); | ||
case NUMBER_VALUE: | ||
return value.getNumberValue(); | ||
case STRING_VALUE: | ||
return value.getStringValue(); | ||
case BOOL_VALUE: | ||
return value.getBoolValue(); | ||
case NULL_VALUE: | ||
return null; | ||
default: | ||
throw new IllegalArgumentException("Unknown Value type: " + value.getKindCase()); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This shouldn't have "Cluster" in its name. It is for all xds metadata.